AngularJS单元测试:注入服务未定义

时间:2015-08-28 01:18:28

标签: javascript angularjs unit-testing

由于喷油器没有正确喷射,我很难对angularJS进行单元测试。我注入的任何东西都是undefined

模块:

app.module.js

angular.module('myServices', []);

AngularTest.js

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
      });

    });
});

我已在主模块中成功测试过控制器,但是,这个特殊模块正在测试我的理智。

有什么想法吗?

编辑:

Karma文件:

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:

Karma似乎也没有抱怨:从日志中来看:

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

0 个答案:

没有答案