Reputation: 63
I have a controller defined in a directive and having trouble unit testing it. Is this possible without globalizing or separating the controller from the directive?? Can you add a simple example??
Upvotes: 4
Views: 2259
Reputation: 893
In your case you could test the elements controller by accessing controllers functions from the compiled elements scope.
The easiest way to access the scope of the element is by calling the #scope() function on the compiled angular element.
it ('should have a function X on scope', inject(function($rootScope, $compile) {
var element = $compile('<div test-directive></div>')($rootScope);
expect(element.scope().myFunction).toEqual(jasmine.any(Function));
});
Heres a simple example of the following technique used in a jsFiddle.
Upvotes: 3