0

我在一个基于 的应用程序上工作Angular 1.5angular-ui-router并且WebPack我想对模块声明部分进行单元测试,尤其是在state配置方面(我想测试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...
        });
    });
});

这是我的应用程序模块:

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();
                    }                        
                }
            })
    });

编辑

我也尝试了本教程(这正是我试图实现的目标),但效果并不好。

4

0 回答 0