![](/img/trans.png)
[英]ui-router test failed with event.preventDefault() in $stateChangeStart
[英]Test ui-router and stateChangeStart
這是我的生產代碼:
myApp.run ($rootScope, $location, AuthService, $state) ->
$rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) ->
$rootScope.stateChangeStarthasBeenCalled = true
if toState.authenticate is true
$rootScope.authNecessaryButNotProvided = true
我的路由器的一部分,我在其中設置了authenticate
.state('auth.profile',
url : 'someURL'
templateUrl : 'some.html'
controller : 'AuthController'
authenticate: true
)
我這樣測試:
it 'should have stateChangeStarthasBeenCalled defined', ->
mockRootScope.$broadcast("$stateChangeStart", "event", "toState:auth.profile", "toParams", "fromState", "fromParams")
expect(mockRootScope.stateChangeStarthasBeenCalled).toBeTruthy()
it 'should have authNecessaryButNotProvided defined', ->
mockRootScope.$broadcast("$stateChangeStart", "event", "toState:auth.profile", "toParams", "fromState", "fromParams")
expect(mockRootScope.authNecessaryButNotProvided).toBeTruthy()
我想檢查toState
對象,例如我在路由中定義的toState.authenticate
。
因此,作為前提條件,如何告訴stateChangeStart
我們要更改為的狀態?
在生產中,代碼按預期工作,在測試中,工作不正常。
您可以手動為事件指定參數,這些參數將傳遞給事件處理程序:
mockRootScope.$broadcast(
'$stateChangeStart',
{ name: 'auth.profile', authenticate: true }, // toState
{}, // toParams
{}, // fromState
{} // fromParams
);
它們將以相同的順序出現,但是第一個參數將保留給event
對象:
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
// ----------------------------------------^
// first argument "event" is auto-generated
console.log(toState.authenticate); // true
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.