请求$ state.go不在单元测试中工作

时间:2017-04-06 15:56:34

标签: angularjs unit-testing webpack angular-ui-router jasmine2.0

我在基于Angular 1.5angular-ui-routerWebPack的应用上工作,我想对模块声明部分进行单元测试,特别是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(这正是我试图实现的目标),但效果并不好。

0 个答案:

没有答案