javascript AngularJS 中的测试:注入函数的引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14762605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tests in AngularJS: Reference error of the inject function
提问by ChriX
I try to test the code below:
我尝试测试下面的代码:
describe('myService test', function () {
describe('when I call myService.one', function () {
beforeEach(angular.module('TargetMarketServices'));
it('returns 1', inject(function (imagesRepository) {
expect(true).toEqual(true);
}));
});
});
When this code is executed I get this error:
执行此代码时,我收到此错误:
TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)')
at http://localhost:8080/testacular.js:76
at http://localhost:8080/context.html:35
ReferenceError: Can't find variable: inject
at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:6
at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:8
at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:10
PhantomJS 1.8: Executed 1 of 3 (1 FAILED) (skipped 2) (0.072 secs / 0.01 secs)
PhantomJS 1.8:执行 1 of 3 (1 FAILED)(跳过 2)(0.072 秒 / 0.01 秒)
For my test, I use Testacular with Jasmine and PhantomJS.
对于我的测试,我将 Testacular 与 Jasmine 和 PhantomJS 一起使用。
回答by user2747330
AngularJS provides two testing libraries:
AngularJS 提供了两个测试库:
- angular-mocks.js
- angular-scenario.js
- angular-mocks.js
- angular-scenario.js
angular-mocks is used for Jasmine and Karma testing. It publishes global methods module() and inject() to be used in your Jasmine spec tests. This means you must load the angular-mocks.js script (after you load the angular.js library/script)
angular-mocks 用于 Jasmine 和 Karma 测试。它发布了用于 Jasmine 规范测试的全局方法 module() 和 inject() 。这意味着您必须加载 angular-mocks.js 脚本(在加载 angular.js 库/脚本之后)
angular-scenario is only used for e2e testing.
angular-scenario 仅用于 e2e 测试。
回答by Ryan O'Neill
The line where you have
你所在的那条线
beforeEach(angular.module('TargetMarketServices'));
should be
应该
beforeEach(module('TargetMarketServices'));
If you take a look at the angular-phonecatproject in test/unit/directivesSpec.js it uses
如果您查看test/unit/directivesSpec.js中的angular-phonecat项目,它使用
beforeEach(module('myApp.directives'));
If I modify it to use angular.module instead:
如果我修改它以使用 angular.module 代替:
beforeEach(angular.module('myApp.directives'));
then I get this error when running testacular also:
然后我在运行 testacular 时也收到此错误:
TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)')