How to automatically load a module in AngularJS wi

2019-06-07 06:08发布

ngMock does some magic to automatically include itself if you include angular-mocks.js in your index.html.

What's the simplest way to force angular to load a module in test mode simply by including a file and not having to edit any module dependencies.

1条回答
孤傲高冷的网名
2楼-- · 2019-06-07 06:21

The only way to load a module is by calling angular.module(...). ngMocks loads "itself" by calling:

angular.module('ngMock', ['ng']).provider(...).config(...);

You don't need to declare a module as a dependency to load it. You can just include angular.module('<moduleName>', [<moduleDependencies>...]); in its script.


If you mean "how is ngMock automagically added to the dependency list of any module loaded using window.module or angular.mock.module, it is because ngMocks creates a custom injector, such that it takes the list of dependencies and prepends 'ngMock':

window.inject = angular.mock.inject = function() {
  ...
  return isSpecRunning() ? workFn() : workFn;
  ...
  function workFn() {
    var modules = currentSpec.$modules || [];

    modules.unshift('ngMock');   // <-- this line does the trick
    modules.unshift('ng');
    ...

You could create your own function that prepends your module in the list of dependencies before instantiating, but I hardly believe this will help in testing. On the contrary, it will be one more source of errors (and will result in possibly "hiding" dependency errors).

查看更多
登录 后发表回答