How can I unit test a function inside a directive's link? I tried something like this, but not worked:
directive:
app.directive("hello", function(){
return {
link: function(scope){
scope.hello = function(){
return "hello";
};
}
};
});
unit test:
describe("Hello directive", function(){
var compile, scope;
beforeEach(inject(function($compile, $rootScope){
scope = $rootScope.$new();
compile = $compile;
}));
it("should return hello", function(){
var element = compile("<hello></hello>")(scope);
expect(element.scope.hello()).toBe("hello");
});
});
element.scope.hello()and not simplyscope.hello()? Also, element.scope is a function returning the scope of the element, as documented: docs.angularjs.org/api/ng/function/angular.element. So it should beelement.scope().hello().<div hello></div>for this directive to be applied. Set the restrict field to 'E' if you want the directive to be applied using<hello></hello>