如何使用$ stateChangeStart或其他发出的事件进行测试?
我有以下代码,实质上检查用户是否已登录,如果没有,则重定向到app.login
状态
app.run(function ($rootScope, $state, AuthenticationService) {
$rootScope.AuthenticationService = AuthenticationService
$rootScope.isLoggedIn = AuthenticationService.getIsLoggedIn
if (!$rootScope.isLoggedIn()) {
$state.go('app.login')
}
// Catch all errors on state change
$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
$state.go('app.home')
})
// Sets up the role requirements per state
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (AuthenticationService.getIsLoggedIn()) {
} else {
if (toState && toState.name !== 'app.login') {
$state.go('app.login')
}
}
})
})
我想要实现的测试:
'use strict'
describe('Controller', function () {
var $scope
, $state
, $rootScope
, AuthenticationService
, $controller
beforeEach(module('replanApp'))
beforeEach(inject(function ($injector) {
$state = $injector.get('$state')
$rootScope = $injector.get('$rootScope')
AuthenticationService = $injector.get('AuthenticationService')
$scope = $rootScope.$new()
$controller = $injector.get('$controller')
}))
describe('Initializers', function () {
it('should redirect to /login if the user is not logged in', function () {
$state.go('app.admin.index')
$rootScope.$apply()
assert.notOk(AuthenticationService.getIsLoggedIn())
assert.equal($state.current.name, 'app.login')
})
})
})
它基本上应该进入状态,然后$rootScope.$on('$stateChangeStart', fn(){})
应该看到用户没有登录,并将它们转移到app.login
状态。
但我得到了AssertionError: expected 'app.admin.index' to equal 'app.login'
如何用$ stateChangeStart和其他事件实现测试?
答案 0 :(得分:2)
我会修改测试以检查是否已使用$state.go
调用'app.login'
,而不是尝试检查当前状态:
describe('Initializers', function () {
beforeEach(function(){
spyOn($state,'go');
});
it('should redirect to /login if the user is not logged in', function () {
$state.transitionTo('app.admin.index');
$rootScope.$apply();
assert.notOk(AuthenticationService.getIsLoggedIn());
expect($state.go).toHaveBeenCalledWith('app.login');
});
});
我无法承诺会修复您的测试,但它会更多地进行单元测试,因为它不依赖$state.go
来设置当前状态。