我自己找到的。在查看了角度ui路由器源代码后,我在
ui-sref指令中找到了以下行:
// angular-ui-router.js#2939element.bind("click", function(e) { var button = e.which || e.button; if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) { // HACK: This is to allow ng-clicks to be processed before the transition is initiated: $timeout(function() { $state.go(ref.state, params, options); }); e.preventDefault(); }});当元素收到点击时,
$state.go会包装在
$timout回调中。因此,在测试中,您必须注入
$timeout模块。然后
$timeout.flush()像这样做:
element.find('a').click();$timeout.flush();expect($state.is('someState')).toBe(true);


