我正忙於單元測試與業力。我想檢查狀態轉換,所以我創建了這個測試。該應用程序首先進入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