2017-03-07 114 views
0

我使用噶寫角測試:這是我spec.js文件:角測試stateprovider總是返回null

'use strict'; 
var state; 
var $rootScope, $state, $injector, CategoriesService; 

describe('Categories', function() { 
describe('Categories Manage', function() { 

    beforeEach(function() { 
     beforeEach(module('ui.router')); 
     module('CL.Categories', function($provide) { 
      $provide.value('CategoriesService', CategoriesService = {}); 
     }); 

     inject(function(_$rootScope_, _$state_, _$injector_, $templateCache, _CategoriesService_) { 
      $rootScope = _$rootScope_; 
      $state = _$state_; 
      $injector = _$injector_; 
      CategoriesService = _CategoriesService_; 

      // We need add the template entry into the templateCache if we ever 
      // specify a templateUrl 
      $templateCache.put('layout/dashboard.html', ''); 
      $templateCache.put('layout/sidebar.html', ''); 
      $templateCache.put('layout/footer.html', ''); 
      $templateCache.put('layout/header.html', ''); 
      $templateCache.put('categories/manage/index.html', ''); 

     }); 
    }); 

    it('should respond to URL with query parameters', function() { 
     expect($state.href(state)).toEqual('#/categories/manage'); 
    }); 
}); 

});

這是我的配置文件:

(function(){ 
'use strict'; 

angular.module('CL.Categories') 
    .config(['$stateProvider', categoriesConfig]); 

function categoriesConfig ($stateProvider){ 
    $stateProvider 
     .state('dashboard.selectCategories', { 
      url: "/categories/select?criteria&referrer&index", 
      templateUrl: 'categories/select/index.html', 
      controller: 'SelectCategoriesController', 
      controllerAs: 'vm', 
      resolve: { 
       categoryRoot: ['CategoriesService', function(CategoriesService){ 
        return CategoriesService.getRootCategories(); 
       }] 
      } 
     }) 
     .state('dashboard.manageCategories', { 
      url: "/categories/manage?active", 
      templateUrl: 'categories/manage/index.html', 
      controller: 'ManageCategoriesController', 
      controllerAs: 'vm', 
      resolve: { 
       workCategories: ['CategoriesService', function (CategoriesService) { 
        return CategoriesService.getCategoriesByOrganisationWithCertificates(); 
       }] 
      } 
     }); 
} 
})(); 
我karma.config文件

我的基本路徑設置爲'./'。測試預期($ state.href(州))總是當你讓你的模擬電話的狀態返回null

回答

0

,試試這個,而不是讓「國家」:

state = $state.get('dashboard.selectCategories'); 

然後你就可以試件狀態聲明:

expect(state.url).toEqual('/categories/select?criteria&referrer&index'); 
expect(state.templateUrl).toEqual('categories/select/index.html'); 

等等......

+0

我試圖放置$ state.get聲明在注入功能,外beforeEach和它上面的聲明,並始終狀態爲空 – bilpor

+0

我不得不移動上述外部描述的變量聲明,但是在循環的哪裏嘗試'$ state.go','$ state.get'或$ state.transitionTo'我總是返回'沒有這樣的狀態。' – bilpor

+0

將$ state.get()語句移到it()語句中。就個人而言,我會將每個'狀態'測試包裝在它自己的describe塊中,用它自己的beforeEach()塊等, – rrd