我正在尝试使用类似于https://stackoverflow.com/a/21078955/270511建议的ui-router为Angular应用编写测试。
it('should transition to state', inject(function($rootScope, $state, $log) {
$rootScope.$on('$stateChangeError', function(ev, toState, toParams, fromState, fromParams, error) {
$log.error('$stateChangeError: ', error);
});
$rootScope.$on('$stateNotFound', function(ev, toState, toParams, fromState, fromParams, error) {
$log.error('$stateNotFound: ', error);
});
var caseId = 1;
$state.go('case.summary', { id: caseId }, { notify: true, reload: true });
$rootScope.$digest();
$log.debug('$state.current.name: ' + $state.current.name);
expect($state.current.name).toEqual('case.summary'); // fails
expect($state.is('case.summary', { id: caseId })).toBeTruthy(); // fails
}));
但是这个测试失败了。
summary section
✓ should respond to URL
LOG LOG: 'debug: caseSvc.get, url: ,http://52.2.224.42:3000/cases/1'
LOG LOG: 'debug: $state.current.name: '
✗ should transition to state
Expected '' to equal 'case.summary'.
然而,这个测试成功了:
it('should respond to URL', inject(function($state) {
expect($state.href('case.summary', { id: 1 })).toEqual('#/cases/1/summary');
}));
目前正在尝试在Chrome调试器中跟踪此内容。
我现在很难过。
答案 0 :(得分:0)
$ state.go()返回需要等待的承诺。
而不是
$state.go('case.summary', { id: caseId }, { notify: true, reload: true });
expect ...
我不得不写
$state.go(stateName, { id: caseId }, { notify: true, reload: true })
.then(function() {
$rootScope.$digest();
$log.debug('$state.current.name: ' + $state.current.name);
expect($state.current.name).toEqual(stateName);
expect($state.is(stateName, { id: caseId })).toBeTruthy();
});
事实上,$ state.go的第三个参数也是不必要的。