我正忙于单元测试与业力。我想检查状态转换,所以我创建了这个测试。该应用程序首先进入landing
状态,从那里我想去main
。这是一个安全的路由,这样被重定向到login
:如何以角度单位测试statetransition?
it('should go to loginpage before main', function() {
$scope.$apply();
expect($state.current.name).toBe('landing');
$state.transitionTo('main');
$scope.$apply();
expect($state.current.name).toBe('login');
});
在我app.js我:
$rootScope.$on('$stateChangeStart', function (event,next) {
console.log('statechange from:', $state.current.name)
//console.log('statechange to:',$state)
if (!next.authenticate) {
return;
}
//if next state is secure then check whether user is logged in:
if (next.authenticate) {
event.preventDefault();
$state.transitionTo('login');
console.log('goto login');
}
});
当我运行我的测试与因果报应,我得到:
Chrome 51.0.2704 (Mac OS X 10.11.4) secure tests should go to loginpage before main FAILED
Expected '' to be 'landing'.
at Object.<anonymous> (/Users/dimitri/karma/basic_karma/appSpec.js:23:37)
Expected '' to be 'login'.
为什么$state.current.name
是空的?
github上裁判:https://github.com/dimitri-a/basic_karma