1

我有一个看起来像这样的单元测试

describe('Interceptor: myInterceptor', inject(function($rootScope){
    var rootScope, routeParams = { id: null };

    beforeEach(module('MyApp', function ($provide) {
        $provide.factory('$routeParams', function () {   // mock $routeParams
            return routeParams;
        });

        rootScope = $rootScope.$new();
        $provide.value('$rootScope', rootScope);         // mock $rootScope

    }));
    ....
}));

但是,当我执行上面显示的“inject(function($rootScope){ ..”时,我收到以下错误(使用 Karma 和 PhantomJS):

PhantomJS 1.9.2 (Mac OS X) Interceptor: myInterceptor encountered a declaration exception FAILED
TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
    at workFn (/dev/myapp/app/bower_components/angular-mocks/angular-mocks.js:2072)
    at /dev/myapp/test/spec/interceptors/my-interceptor.js:67
    ....
4

1 回答 1

5

我认为调用时注入不起作用describe。我遇到了同样的错误,我通过将注入从我describe的调用移动到调用来解决beforeEach

var rootScope;
beforeEach(inject(function ($rootScope) {
    rootScope = $rootScope.$new();
}));

你可以有多个beforeEach,所以只需将这个添加到现有的beforeEach.

于 2013-11-26T12:30:23.587 回答