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
}));
....
}));
然而,當我「注入(函數($ rootScope){..」正如我上面顯示,我得到以下錯誤(使用因果報應和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
....
我的問題是潛在的相關:http://stackoverflow.com/questions/15416006/how-do-i-inject-rootscope-into-an-angularjs-unit-test –