Angularjs 单元测试角度ui路由器最大调用堆栈
我是angular ui router的新手,我一直在尝试对基本身份验证进行一些单元测试,在遇到最大调用堆栈错误之前,它工作正常 我已将错误缩小到app.run部分中的$state.go调用。 我去掉这个,测试就开始了。但是它破坏了我的应用程序 我能做些什么来解决这个问题?所以我可以测试这个部分,使它也工作? 为什么这项工作正常,但会导致测试错误 错误:Angularjs 单元测试角度ui路由器最大调用堆栈,angularjs,jasmine,angular-ui-router,Angularjs,Jasmine,Angular Ui Router,我是angular ui router的新手,我一直在尝试对基本身份验证进行一些单元测试,在遇到最大调用堆栈错误之前,它工作正常 我已将错误缩小到app.run部分中的$state.go调用。 我去掉这个,测试就开始了。但是它破坏了我的应用程序 我能做些什么来解决这个问题?所以我可以测试这个部分,使它也工作? 为什么这项工作正常,但会导致测试错误 错误: RangeError: Maximum call stack size exceeded at Scope.$broadcast (/Use
RangeError: Maximum call stack size exceeded
at Scope.$broadcast (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular/angular.js:12876:15)
at Object.transitionTo (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular-ui-router/release/angular-ui-router.js:2584:24)
at Object.go (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular-ui-router/release/angular-ui-router.js:2454:21)
at /Users/paulrobinson/Workspace/contactCachePOC/dev/js/core.js:9:5889
at Scope.$broadcast (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular/angular.js:12874:28)
at Object.transitionTo (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular-ui-router/release/angular-ui-router.js:2584:24)
at Object.go (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular-ui-router/release/angular-ui-router.js:2454:21)
at /Users/paulrobinson/Workspace/contactCachePOC/dev/js/core.js:9:5889
at Scope.$broadcast (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular/angular.js:12874:28)
at Object.transitionTo (/Users/paulrobinson/Workspace/contactCachePOC/dev/bower_components/angular-ui-router/release/angular-ui-router.js:2584:24)
代码:
超过最大调用堆栈表明正在递归地发生某些事情。我猜您将要启动的状态也未被授权,因此您会被重定向到重新启动,等等
您需要使AuthService.IsAuthorized在某个点返回true,以便确定它似乎是ui路由器的错误 这里有一个解决方案 上面的代码将在不破坏浏览器或单元测试的情况下处理该问题。 使用此修复程序时请小心。使用$state.gomain,{},{notify:false};用于通知$stateChangeStart事件
先生,您说得对,我在$state前面放了一个控制台日志。去获取当前状态,当前状态总是空的!console.logJSON.stringify$state;日志:{params:{},当前:{name:},url:^,视图:null,抽象:true},$current:{self:{name:,url:^,视图:null,抽象:true},解析:{},url:{segments:[],params:{},sourcePath:,sourceSearch:,regexp:{},前缀:},可导航:null,参数:{},视图:{},ownParams:[],路径:],包括:{true},局部:{resolve:null,全局:{states:},transition“嗯,但是如果有一个循环,那么为什么我仍然可以实际使用该站点并转换到其他状态,而不让浏览器不断输出日志条目呢?因为在真实的网站上,您的auth服务工作正常,但在测试中,您伪造authservice以始终返回false:spyOnAuthService,isAuthorized.andCallFakefunction状态{return false;}
app.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/');
var access = routingConfig.roles;
$stateProvider
.state('start', {
url : '/',
templateUrl : 'partials/decide.html',
controller : 'decideController',
data: {
access: access.anon
}
});
}]);
app.run(['$rootScope', '$state', 'AuthService', '$log', '$location',
function ($rootScope, $state, AuthService, $log, $location) {
$rootScope.$on("$stateChangeStart",
function (event, toState, toParams, fromState, fromParams) {
if (!AuthService.isAuthorized(toState.data.access)) {
event.preventDefault();
$rootScope.error = null;
//STATE.GO is causing the error
$state.go('start');
//$location.path('/#/');
return;
}
});
}]);
describe('Test as an anonymous user', function () {
var $templateCache, $state, $stateParams, $rootScope, $httpBackend,
AuthService,, $location;
var roles = {
anon: { id: 0, value: 'Public'},
user: { id: 1, value: 'User'}
};
beforeEach(module('app'));
beforeEach(inject(function(_$templateCache_, _$state_, _$stateParams_, _$rootScope_, _$httpBackend_,
_AuthService_, _sessionService_, _$location_) {
$templateCache = _$templateCache_;
$state = _$state_;
$stateParams = _$stateParams_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
AuthService = _AuthService_;
$location = _$location_;
//Fake it and say we're not authorized.
spyOn(AuthService, "isAuthorized").andCallFake(function (state){
return false;
});
}));
describe('View page.', function () {
beforeEach(function () {
$state.go('start', { });
$rootScope.$apply();
});
it('Should view page.', function () {
expect($state.current.name).toEqual('start');
});
});
});
$state.go(toState.name, toParams, {notify: false}).then(function() {
$rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
});
$state.go("main", {}, {notify:false});