Words Like Jared Words Like Jared - 1 year ago 80
AngularJS Question

Unit Testing AngularJS directive with templateUrl

I have an AngularJS directive that has a

defined. I am trying to unit test it with Jasmine.

My Jasmine JavaScript looks like the following, per the recommendation of this:

describe('module: my.module', function () {

describe('my-directive directive', function () {
var scope, $compile;
beforeEach(inject(function (_$rootScope_, _$compile_, $injector) {
scope = _$rootScope_;
$compile = _$compile_;
$httpBackend = $injector.get('$httpBackend');

describe('test', function () {
var element;
beforeEach(function () {
element = $compile(

afterEach(function () {

it('test', function () {


When I run this in my Jasmine spec error I get the following error:

TypeError: Object #<Object> has no method 'passThrough'

All I want is for the templateUrl to be loaded as is - I don't want to use
. I believe this may be related to it using ngMock instead of ngMockE2E. If this is the culprit, how do I use the latter instead of the former?

Thanks in advance!

Answer Source

What I ended up doing was getting the template cache and putting the view in there. I don't have control over not using ngMock, it turns out:

beforeEach(inject(function(_$rootScope_, _$compile_, $templateCache) {
    $scope = _$rootScope_;
    $compile = _$compile_;
    $templateCache.put('path/to/template.html', '<div>Here goes the template</div>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download