Angularjs 角度测试stateprovider始终返回null
我正在使用Karma编写角度测试:这是我的spec.js文件:Angularjs 角度测试stateprovider始终返回null,angularjs,jasmine,Angularjs,Jasmine,我正在使用Karma编写角度测试:这是我的spec.js文件: 'use strict'; var state; var $rootScope, $state, $injector, CategoriesService; describe('Categories', function() { describe('Categories Manage', function() { beforeEach(function() { beforeEach(module('ui.r
'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文件中,我将基本路径设置为。/'。testexpect($state.href(state))总是返回null当您对state进行模拟调用时,请尝试此操作以获取“state”:
state = $state.get('dashboard.selectCategories');
然后,您可以对国家声明进行测试:
expect(state.url).toEqual('/categories/select?criteria&referrer&index');
expect(state.templateUrl).toEqual('categories/select/index.html');
依此类推……我尝试将$state.get语句放在注入函数中,外部beforeach语句放在it语句之上,并且总是state为null。我必须将变量声明移到外部descripe语句之上,但在循环中的何处尝试任何“$state.go”都无关紧要,“$state.get”或“$state.transitiono”我总是返回
没有这样的状态。
将$state.get()语句移到it()语句中。就我个人而言,我会将每个“state”测试包装在它自己的descripe块中,使用它自己的beforeach()块等,state仍然为null:(这几乎就像它没有将配置文件作为参考点一样。您可能必须执行angular.mock.module('ui.router');(并导入它)