$ injector不能在我的测试中工作么?

时间:2014-12-22 18:39:46

标签: javascript angularjs unit-testing jasmine karma-runner

- $ injector在我的测试中不起作用? -

为什么它无法退回我的服务?

foo.service.js:

'use strict';

angular.module('fooApp')
.factory('FooService', function () {

    var function1 = function function1(value) {
        return value;
    };

    return {
        function1 : function1
    };
});

我的测试(foo.service.spec.js)

'use strict';

describe('Service: FooService', function () {
beforeEach(module('fooApp'));

it('should...', inject(function(FooService) {
    expect(FooService.function1('...')).toEqual(...);
    ....

}));
....
});

当我评估FooService未定义时。为什么?

错误堆栈跟踪:

...
TypeError: 'undefined' is not an object (evaluating 'FooService.function1')
...

新的堆栈跟踪错误:

Error: [$injector:modulerr] Failed to instantiate module fooApp due to:
TypeError: 'undefined' is not a function (evaluating '$stateProvider
            .state')
    at C:/_Projet/foo/client/app/auth/auth.js:15
    at invoke (C:/_Projet/foo/client/bower_components/angular/angular.js:4182)
    at runInvokeQueue (C:/_Projet/foo/client/bower_components/angular/angular.js:4088)
    at C:/_Projet/foo/client/bower_components/angular/angular.js:4097
    at forEach (C:/_Projet/foo/client/bower_components/angular/angular.js:323)
    at loadModules (C:/_Projet/foo/client/bower_components/angular/angular.js:4120)
    at createInjector (C:/_Projet/foo/client/bower_components/angular/angular.js:4004)
    at workFn (C:/_Projet/foo/client/bower_components/angular-mocks/angular-mocks.js:2339)
http://errors.angularjs.org/1.3.8/$injector/modulerr?p0=fooApp&p1=TypeError%3A%20'undefined'%20is%20not%20a%20function%20(evaluating%20'%24stateProvider%0A%20%20%20%20%20%20%20%20%20%20%20%20.state')%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fapp%2Fauth%2Fauth.js%3Fb8f7bccdf2d8f57e6d6f0c810e0d1c6f0d3df707%3A15%0A%20%20%20%20at%20invoke%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A4182)%0A%20%20%20%20at%20runInvokeQueue%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A4088)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A4097%0A%20%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A323)%0A%20%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A4120)%0A%20%20%20%20at%20createInjector%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular%2Fangular.js%3Febb86f089066f7089a249e390e64bcab67622158%3A4004)%0A%20%20%20%20at%20workFn%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fclient%2Fbower_components%2Fangular-mocks%2Fangular-mocks.js%3Fb01a9cf8de7de4def103da067347cb07fe247c96%3A2339)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A1145%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2177%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2130%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2460%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2177%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2130%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2606%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2177%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fjasmine.js%3F437512c57124d57f5ec77e2e82bfb4e3cdd15cce%3A2167
    at C:/_Projet/foo/client/bower_components/angular/angular.js:4118
    at forEach (C:/_Projet/foo/client/bower_components/angular/angular.js:323)
    at loadModules (C:/_Projet/foo/client/bower_components/angular/angular.js:4120)
    at createInjector (C:/_Projet/foo/client/bower_components/angular/angular.js:4004)
    at workFn (C:/_Projet/foo/client/bower_components/angular-mocks/angular-mocks.js:2339)

1 个答案:

答案 0 :(得分:1)

如果您使用的是Angular mocks,则应使用下划线包装来解析https://docs.angularjs.org/api/ngMock/function/angular.mock.inject

中提到的引用
'use strict';

describe('Service: FooService', function () {
    var FooService;

    beforeEach(function(){
        module('fooApp');
        inject(function (_FooService_) {
            FooService = _FooService_;
        });
    });

....
});

注意:此帖子的上一版本有两次beforeEach()次来电。在模块初始化之前,注射可能正在运行。上面编辑的版本将模块初始化移动到单个beforeEach()

在使用注入时,您也可以尝试其他方法,并将FooService与测试一起注入:

'use strict';

describe('Service: FooService', function () {
    beforeEach(module('fooApp'));

    it('should...', inject(function(FooService) {
        expect(FooService.function1('...')).toEqual(...);
        ....

    }));
....
});