I am writing a unit test for my ui-router routes. However, I cannot make it pass.
Here's my test, and the $state.current.name
is giving me frontpage
which is the otherwise
state.
it('should change state to items', function() {
$state.go('items');
$rootScope.$digest();
expect($state.current.name).toBe('items');
})
The state:
$urlRouterProvider.otherwise("/");
$stateProvider
.state('items', {
url: '/items',
templateUrl: 'js/modules/items/partials/items.html',
controller: 'ItemsController',
resolve: {
items : ['ItemService', '$q', function(ItemService, $q) {
var defer = $q.defer();
ItemService.getItems().success(function(data) {
defer.resolve(data);
});
return defer.promise;
}]
}
})
.state('frontpage', {
url: '/',
template: 'Frontpage'
})
Why am I getting the frontpage
state after calling $state.go('items')
?
Inject $q, then try $q.flush(); after changing state.
在花费了几个小时之后,我最终使用了$timeout.flush()
,我想这将清除所有的promise队列。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.