我有一个简单的单元测试。
我正在尝试注入服务SearchPageService
,但我得到
无法读取未定义的属性“ load”
所以我猜customDataService
为空,因为我没有在任何地方设置它。如何在服务(moduleApp.SearchPageService)中初始化此服务?
我在karma.conf上配置了文件
files: [
'angular.js'
'../app/app.js',
'../components/angular-mocks/angular-mocks.js',
'unit/*.js',
'../app/src/callCenter/page1/searchPageService.js',
'../app/src/callCenter/page1/searchPageController.js'
],
测试
describe('Controller: testController', function() {
beforeEach(function() {
//module('moduleApp', ['moduleApp.moduleDependencies', 'angular.dependencies', 'third.party.dependencies', 'ipp.dependencies']);
module('moduleApp.controller');
module('moduleApp.services');
module('custom.services');
});
var ctrl;
var $scope;
var service;
var customDataService;
var searchPageService;
beforeEach(inject(function($controller, $rootScope, $http) {
$scope = $rootScope.$new();
console.log('*** IN INJECT!!***: ', service);
ctrl = $controller(SearchPageController, {
$scope: $scope,
'moduleApp.SearchPageService': service,
CustomDataService: customDataService,
$http: $http
});
}));
it('should have $scope defined', function() {
expect($scope).toBeDefined();
});
});
控制器
angular.module('moduleApp.controller').controller('moduleApp.SearchPageController', SearchPageController);
SearchPageController.$inject = ['$log', '$scope', 'moduleApp.SearchPageService', '$http'];
function SearchPageController($log, $scope, searchPageService, $http) { //code }
服务:
angular.module('moduleApp.services').service('moduleApp.SearchPageService', SearchPageService);
SearchPageService.$inject = ['$log', 'CustomDataService'];
function SearchPageService ($log, customDataService) {
'use strict';
var Service = function() {
this.load = function load(controller, scope) {
// TODO define configuration
var loadConfig = [
//some Configs
];
return customDataService.load(controller, loadConfig);
};
};
return new Service(); }