23t*_*tux 7 jasmine angularjs angular-ui-router
我正在尝试测试一些视图,这些视图<a ui-sref='someState'>link</a>
用于链接到我的应用程序中的其他状态.在我的测试中,我触发了对这些元素的点击,如下所示:
element.find('a').click()
Run Code Online (Sandbox Code Playgroud)
如果状态切换到,我该如何测试someState
?$state
在我的控制器中使用时会很容易:
// in my view
<a ng-click="goTo('someState')">link</a>
// in my controller
$scope.goTo = function(s) {
$state.go(s)
};
// in my tests
spyOn($state, 'go');
element.find('a').click()
expect($state.go).toHaveBeenCalled()
Run Code Online (Sandbox Code Playgroud)
但是当我使用时,我ui-sref
不知道要窥探什么对象.如何验证我的应用程序处于正确的状态?
23t*_*tux 18
我自己找到了.在查看了角度ui路由器源代码之后,我在ui-sref
指令中找到了这一行:
// angular-ui-router.js#2939
element.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();
}
});
Run Code Online (Sandbox Code Playgroud)
当元素收到点击时,它$state.go
被包装在$timout
回调中.因此,在您的测试中,您必须注入$timeout
模块.然后就这样做$timeout.flush()
:
element.find('a').click();
$timeout.flush();
expect($state.is('someState')).toBe(true);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1602 次 |
最近记录: |