我在基于Angular 1.5
,angular-ui-router
和WebPack
的应用上工作,我想对模块声明部分进行单元测试,特别是state
配置(I想要测试resolve
部分的源代码。
我的应用程序的所有其他单元测试工作正常,但在这个上看起来我的电话$state.go
似乎永远不会被处理:
describe('Simple Module', () => {
beforeEach(angular.mock.module('ui.router', 'my-app'));
let $state;
let $location;
let $httpBackend;
beforeEach(inject((_$location_, _$state_, _$httpBackend_) => {
$state = _$state_;
$location = _$location_;
$httpBackend = _$httpBackend_;
}));
describe('app.onlineListing.error', () => {
it('go to state', () => {
const state = 'app.onlineListing.error';
$state.go(state);
// PROBLEM IS HERE...
// $state.current.name is empty ('') just like if $state.go was never called
// It's not better with $location.url and $httpBackend.flush
// ...
expect($state.current.name).toBe(state);
// I will spy on myService and expect that myService.getData has been called
// but currently state is not reach so resolve parts are never triggered...
});
});
});
这是我的app模块:
export default angular.module('my-app', ['oc.lazyLoad', 'ui.router', mySimpleModule.name])
.config(($stateProvider, ...) => {
...
$stateProvider
.state('app', {
abstract: true,
...
}
});
有mySimpleModule声明:
export default angular.module('my-simple-module', [
'ui.router'
])
.config(($stateProvider) => {
$stateProvider
.state('app.onlineListing', {
url: '/onlineListing',
abstract: true,
views: {
body: {
template: '<ui-view />'
}
}
})
.state('app.onlineListing.error', {
url: '/error',
template: require('./configurationError/configurationError.html'),
controller: 'onlineListingConfigurationErrorCtrl',
controllerAs: 'controller',
resolve: {
// @ngInject
modules: ($q, $ocLazyLoad) => {
return $q((resolve) => {
require(['./configurationError/configurationError.js'], (module) => {
resolve($ocLazyLoad.load({ name: module.default }));
});
});
},
fetchedData: (myService) => {
// Finally I want my test to check if getData is called
return myService.getData();
}
}
})
});
修改
我也尝试this tutorial(这正是我试图实现的目标),但效果并不好。