admin管理员组文章数量:1426507
I have a unit test that looks like this
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
}));
....
}));
However when I do "inject(function($rootScope){ .." as I have shown above, I get the following error (using Karma and 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_ponents/angular-mocks/angular-mocks.js:2072)
at /dev/myapp/test/spec/interceptors/my-interceptor.js:67
....
I have a unit test that looks like this
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
}));
....
}));
However when I do "inject(function($rootScope){ .." as I have shown above, I get the following error (using Karma and 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_ponents/angular-mocks/angular-mocks.js:2072)
at /dev/myapp/test/spec/interceptors/my-interceptor.js:67
....
Share
Improve this question
edited Nov 14, 2013 at 13:35
Jeanluca Scaljeri
asked Nov 14, 2013 at 12:20
Jeanluca ScaljeriJeanluca Scaljeri
29.3k66 gold badges235 silver badges383 bronze badges
1
- 1 My question is potentially related: stackoverflow./questions/15416006/… – Gregory Avery-Weir Commented Aug 11, 2014 at 20:56
1 Answer
Reset to default 5I don't think injection works when calling describe
. I had the same error which I solved by moving the injection from my describe
call to a beforeEach
call:
var rootScope;
beforeEach(inject(function ($rootScope) {
rootScope = $rootScope.$new();
}));
You can have multiple beforeEach
, so just add this one above your existing beforeEach
.
本文标签: javascriptangularjs how to mock rootScope in unit testStack Overflow
版权声明:本文标题:javascript - angularjs: how to mock $rootScope in unit test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745461818a2659357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论