- 如果我的单元测试正在调用控制器中的函数并且该函数正在进行服务调用以获取详细信息。它会调用服务(StationService)吗?
- 我的 Karma 单元测试无法注入 StationService 并且无法调用服务。
我的代码。
/// controller
var policyControllers = angular.module('policyControllers', []);
policyControllers.controller('StationListController', ['$translate', '$scope','$rootScope','$state', 'StationService', 'StationListExportService', function ($translate, $scope, $rootScope, $state, StationService, StationListExportService) {
...
$scope.getFilterDetails = function(StationService, filterDetails ){
StationService.get(filterDetails).$promise.then(function (filteredDetails) {
console.log(" Web services Result - ", JSON.stringify(filteredDetails));
},function(error) {
console.log(" Error ");
});
};
///Service
var policyServices = angular.module('policyServices', ['ngResource']);
policyServices.factory('StationService', ['$resource', function($resource) {
return $resource(policyConfig.mock? 'modules/policymanager/services/mock/stations.json': 'http://10.132.240.25:7640/policy/api/v1/stationpolicy/stations',{},{
get:{method: 'POST',isArray: false, url:'modules/policymanager/services/mock/stations.json'}
});
}]);
/// Unit test
describe('station filter', function(){
var scope;
var ctrl;
var translate, scope, rootScope, state;
var StationService, StationListExportService;
beforeEach(module('policyServices'));
beforeEach(module('policyControllers'));
beforeEach(inject(function(_StationService_, _StationListExportService_, $rootScope, $controller, $translate, $state) {
StationService = _StationService_;
StationListExportService = _StationListExportService_;
translate = $translate;
rootScope = $rootScope;
state = $state;
scope = $rootScope.$new();
ctrl = $controller('StationListController', {$scope: scope});
}));
it('Stations Inject test case', inject(['StationService',function(StationService){
var data = {"recency":"","countries":[],"policies":[],"stations":[{"stationName":"Test"}],"status":"ready","regions":[]};
scope.getFilterDetails(StationService, data);
/// Getting StationService is undifiened
}]));