In our app we have several layers of nested directives. I\'m trying to write some unit tests for the top level directives. I\'ve mocked in stuff that the directive itself ne
Directives are just factories, so the best way to do this is to mock the factory of the directive in using the module
function, typically in the beforeEach
block. Assuming you have a directive named do-something used by a directive called do-something-else you'd mock it as such:
beforeEach(module('yourapp/test', function($provide){
$provide.factory('doSomethingDirective', function(){ return {}; });
}));
// Or using the shorthand sytax
beforeEach(module('yourapp/test', { doSomethingDirective: {} ));
Then the directive will be overridden when the template is compiled in your test
inject(function($compile, $rootScope){
$compile('<do-something-else></do-something-else>', $rootScope.$new());
});
Note that you need to add the 'Directive' suffix to the name because the compiler does this internally: https://github.com/angular/angular.js/blob/821ed310a75719765448e8b15e3a56f0389107a5/src/ng/compile.js#L530