I have an angular 1.5 project with many modules and each module may depend on other modules. Trying to unit test say a controller which is part of a module I would do import the module like this:
angular.mock.module('SaidModule');
...then provide and inject its services where needed.
The problem is that SaidModule
depends on AnotherModule1
, AnotherModule2
, AnotherModule3
....
angular.module('SaidModule', ['AnotherModule1', 'AnotherModule2', 'AnotherModule3']);
So naturally when I call SaidModule the other modules are also invoked which is out of scope in terms of Unit testing
In the unit test I have tried the following solution
angular.module('AnotherModule1',[]);
angular.module('AnotherModule2',[]);
angular.module('AnotherModule3',[]);
angular.mock.module('SaidModule');
and although for the current unit test I have successfully decoupled the dependencies I have also destroyed the actual AnotherModule1, AnotherModule2, AnotherModule3 so when its there turn to be unit tested they are not even visible in the angular project which seems correct to me. as I am using angular.module to define a new module which just happens to override the actual module. This solution though is also suggested here mocking module dependencies
In the angular docs it states see angular docs mock module If an object literal is passed each key-value pair will be registered on the module via $provide.value, the key being the string name (or token) to associate with the value on the injector.
So it seems to me that the solution is using somehow angular.mock.module somehow to override the dependent modules but so far I have not found a solution. Any help much appreciated