【发布时间】:2015-04-23 22:05:45
【问题描述】:
我的角度测试有问题。
describe('Routes test', function() {
//Initialize global variables
var $state,
$location;
// Load the main application module
beforeEach(module("MyApp"));
beforeEach(inject(function(_$location_, _$state_, $templateCache) {
$state = _$state_;
$location = _$location_;
// We need add the template entry into the templateCache if we ever
// specify a templateUrl
$templateCache.put('signin.admin.view.html', 'signin');
}));
it('should be redirect to signin if user is not loggedin', function() {
$rootScope.$apply(function() {
$location.path('/');
});
expect($state.current.name).toEqual('signin');
console.log($state.current);
});
});
所以我期待登录状态,因为用户没有登录。但是从控制台 $state.current 仍然是:
Object{name: '', url: '^', views: null, abstract: true}
为了重定向我使用了这个代码:
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toState.name.indexOf('signin') === 0 ) {
if( loggedIn ) {
event.preventDefault();
$state.go('layout.dashboard');
}
} else {
if( !loggedIn ) {
event.preventDefault();
$state.go('signin');
}
}
});
变量 loggedIn 为假。所以我不明白,当我在浏览器中手动进行测试时 $state.current 符合预期。但不在本次测试中。
感谢您的帮助。
【问题讨论】:
标签: angularjs angular-ui-router karma-jasmine