如何对ui-router状态的输入和解析进行单元测试

时间:2015-01-22 16:16:17

标签: javascript angularjs unit-testing jasmine angular-ui-router

我正在尝试测试从以下状态的onEnter调用的Angular UI Bootstrap模式:

.state("profile.index.edit.services", {
        url: "/edit/services/:serviceCode",
        parent:"profile.index",
        onEnter: ['$stateParams', '$state', '$modal',function($stateParams, $state, $modal) {
            $modal.open({
                templateUrl: 'profile/edit-services-modal.html',
                controller: 'ProfileEditServicesController',
                resolve: {
                    profileData: function(CacheService){
                        return CacheService.getItem(CacheService.Items.Profile.loadedProfile);
                    },
                    allServicesList: function(ProfileService){
                        return ProfileService.getAllServicesList($stateParams.serviceCode,$stateParams.orgId);
                    },
                    serviceCode: function() {
                        return $stateParams.serviceCode;
                    }
                }                   
            }).result.then(function(result) {
                if (result) {
                    return $state.transitionTo("profile.index", { orgId: $stateParams.orgId });
                }
                else { // cancel
                    return $state.transitionTo("profile.index", { orgId: $stateParams.orgId }); // don't reload
                }
            }, function () {
                // executes on close/cancel/ESC
                return $state.transitionTo("profile.index", { orgId: $stateParams.orgId });
            });
        }]
    })  

我一直在努力争先恐后地尝试许多不同的事情来设置测试,但似乎无法弄明白。任何掠夺者或建议都非常感谢!!!!

顺便说一句,上面的状态是这些状态的后代,我已经能够为该传递编写测试:

.state('profile.index', {
        url: '/:orgId',
        views: {
            'profile-index@profile': {
                templateUrl: 'profile/view-profile.html',
                controller: 'ProfileController'
            },
            'header@': {
                templateUrl: 'common/layout/header.html',
                controller: 'HeaderController'
            },
            'footer@':{
                templateUrl: 'common/layout/footer.html',
                controller: 'FooterController'
            }
        },
        resolve: {
            profileData: function(ProfileService, $stateParams){
                return ProfileService.getProfile($stateParams.orgId, true);
            }
        }
    })
    .state('profile.index.edit', {
        url: '',
        abstract: true
    })

1 个答案:

答案 0 :(得分:1)

你有没有想过这个?你应该通过一个命名控制器,这样你就可以直接测试那个控制器(因为它只是一个函数),而不是依赖onEnter被激活。