2017-03-07 112 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