簡體   English   中英

如何對onEnter進行單元測試並解決ui路由器狀態

[英]How to unit test onEnter and resolve of ui-router state

我正在嘗試測試從以下狀態的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
    })

你有沒有想過這個? 您應該已經傳遞了一個命名的控制器,這樣您就可以直接測試該控制器(因為它只是一個函數),而不是依賴於被觸發的onEnter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM