ngMock做一些魔法来自动包括本身如果您在您的index.html角mocks.js。
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.
推荐答案
加载模块的唯一方法是通过调用 angular.module(...)
。 ngMocks 加载自己通过调用:
The only way to load a module is by calling angular.module(...)
. ngMocks loads "itself" by calling:
angular.module('ngMock', ['ng']).provider(...).config(...);
您并不需要声明一个模块作为一个依赖加载它。你可以只包括 angular.module('<&MODULENAME GT;',并[d moduleDependencies> ...]);
在脚本
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.
如果你的意思是怎么回事 ngMock
自动地添加到使用任何加载模块的依赖关系列表 window.module
或 angular.mock.module
,这是因为 ngMocks
创建一个定制的喷油器,这样,它需要依赖项列表和prepends 'ngMock
:
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');
...
您可以创建自己的函数,prepends模块中的依赖列表中实例化之前,但我很难相信这将是在帮助测试。相反,这将是错误之一多个源(并导致可能隐藏的依赖错误)。
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).
相关推荐
最新文章