I want to test an AngularJS directive declared like this
app.directive('myCustomer', function() {
return {
template: 'cust.html'
controller: 'customerController'
};
});
In the test I would like to inject (or override) the controller, so that I can test just the other parts of the directive (e.g. the template). The customerController
can of course be tested separately. This way I get a clean separation of tests.
- I have tried overriding the controller by setting the controller property in the test.
- I have tried injecting the
customController
using$provide
. - I have tried setting
ng-controller
on the html directive declaration used in the test.
I couldn't get any of those to work. The problem seems to be that I cannot get a reference to the directive until I have $compile
d it. But after compilation, the controller is already set up.
var element = $compile("<my-customer></my-customer>")($rootScope);