Javascript AngularJS单元测试:未定义注入服务
由于喷油器喷射不正确,我在单元测试angularJS时遇到困难。我注入的任何内容都是Javascript AngularJS单元测试:未定义注入服务,javascript,angularjs,unit-testing,Javascript,Angularjs,Unit Testing,由于喷油器喷射不正确,我在单元测试angularJS时遇到困难。我注入的任何内容都是未定义的 模块: app.module.js angular.module('myServices', []); describe('AngularTest', function() { beforeEach(module('myServices')); var scope; beforeEach(inject(function(_$rootScope_) { //
未定义的
模块:
app.module.js
angular.module('myServices', []);
describe('AngularTest', function() {
beforeEach(module('myServices'));
var scope;
beforeEach(inject(function(_$rootScope_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
scope = _$rootScope_;
}));
describe('Testing Angular injection', function() {
it('scope should be defined', function() {
expect(scope).toBeDefined(); // fails
});
});
});
AngularTest.js
angular.module('myServices', []);
describe('AngularTest', function() {
beforeEach(module('myServices'));
var scope;
beforeEach(inject(function(_$rootScope_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
scope = _$rootScope_;
}));
describe('Testing Angular injection', function() {
it('scope should be defined', function() {
expect(scope).toBeDefined(); // fails
});
});
});
我已经在我的主模块中成功地测试了控制器,但是,这个模块正在测试我的理智
有什么想法吗
编辑:
业力文件:
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-animate/angular-animate.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/app.module.js',
'app/components/**/controller/*.js',
'app/components/**/model/*.js',
'app/components/**/test/unit/*.js'
]
服务模块在app.module.js
中定义,服务位于app/components/services/model/myService.js
(路径似乎不合逻辑,我试图抽象我的结构以尽可能概括这个问题)
编辑2:
业力似乎也没有抱怨:从日志:
28 08 2015 03:38:47.095:WARN [karma]: No captured browser, open http://localhost:9876/
28 08 2015 03:38:47.107:INFO [karma]: Karma v0.13.9 server started at http://localhost:9876/
28 08 2015 03:38:47.151:INFO [launcher]: Starting browser Safari
28 08 2015 03:38:54.790:INFO [Safari 8.0.6 (Mac OS X 10.10.3)]: Connected on socket jhi7nBcWAV_vCJkMAAAA with id 68159057
然后是失败的测试用例:
Safari 8.0.6 (Mac OS X 10.10.3) AngularTest Testing Angular injection scope should be defined FAILED
/path/to/project/app/bower_components/angular/angular.js:68:32
forEach@/path/to/project/app/bower_components/angular/angular.js:336:24
loadModules@/path/to/project/app/bower_components/angular/angular.js:4369:12
createInjector@/path/to/project/app/bower_components/angular/angular.js:4294:22
workFn@/path/to/project/app/bower_components/angular-mocks/angular-mocks.js:2390:60
/path/to/project/app/bower_components/angular/angular.js:4408:30
Expected undefined to be defined.
/path/to/project/app/components/services/test/unit/AngularTest.js:16:34
该死的游猎!
解决方案:将karma配置文件中的浏览器更改为Chrome,调试日志中会显示出更详细的错误信息
结果证明,我的karma
文件中没有包含所需的模块依赖项 它应该很好用。您确定已加载包含myServices模块的文件吗?还有什么错误吗shows@PSL请看一看我的编辑。嗯,我不能对此说太多。我似乎无法复制它@PSL,嗯?把我的头发扯出来。为失败的测试添加了一些Karma输出,但我认为这不会有什么帮助…只是一个很长的机会,但您可以运行Karma w/调试日志级别(您可以在karma配置中或从命令行启用此功能:karma start--log-level debug
。它非常详细,但您可能会看到一些警告/错误,这些都是问题的线索。Firefox也有同样的问题!我希望我能对这个答案进行两次投票!