【发布时间】:2016-07-15 02:29:15
【问题描述】:
我正在使用 karma 进行单元测试。我想检查状态转换,所以我创建了这个测试。该应用程序从进入landing 状态开始,然后我想从那里进入main。这是一条安全路由,因此被重定向到login:
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');
}
});
当我使用 karma 运行测试时,我得到:
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
【问题讨论】:
标签: angularjs unit-testing angular-ui-router karma-jasmine