Reputation: 14250
I am trying to do unit test for my directive.
I have something like
'use strict';
describe('directive test', function () {
var $compile, $rootScope, httpBackend, ngFactory;
beforeEach(module('myApp'));
beforeEach(inject(function (_$compile_, _$rootScope_, _$httpBackend_, _ngFactory_) {
$compile = _$compile_;
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_;
ngFactory = _ngFactory_;
$httpBackend.whenGET('template.html').respond(200);
}));
it('this will setup directive', function() {
var elem = $compile('<my-factory factoryName="name1"></my-factory>')(scope);
scope.$digest();
expect(scope.run).toBe(1);
});
});
My directive
(function(window, angular) {
'use strict';
var app = angular.module('myApp');
app.directive('myFactory', ['ngFactory',
function(ngFactory) {
return {
restrict: 'E',
templateUrl:'template.html',
link: function(scope, elem, attrs) { //not cover
scope.test = function() { //not cover
//do stuff here //not cover
}; //not cover
} //not cover
};
}
]);
})(window, angular);
For some reason, my unit test doesn't cover the link function. I am not sure what I do wrong. Can someone please help me out? Thanks!
Upvotes: 0
Views: 22
Reputation: 21209
You will also need to flush the template request:
it('this will setup directive', function() {
var elem = $compile('<my-factory factoryName="name1"></my-factory>')(scope);
$httpBackend.flush();
scope.$digest();
expect(scope.run).toBe(1);
});
Make sure the correct template is loaded and rendered.
Upvotes: 1