Karma单元测试 - 调用控制器的功能但不能注入服务对象

时间:2015-02-03 13:49:01

标签: angularjs unit-testing karma-mocha

  1. 如果我的单元测试是调用控制器中的函数,并且该函数正在进行服务调用以获取详细信息。它会调用服务(StationService)吗?
  2. 我的Karma单元测试无法注入StationService而无法调用服务。
  3. 我的代码。

    /// 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 
    
      }]));
    

1 个答案:

答案 0 :(得分:0)

尝试仅使用一个应用程序说policyServices并使用相同的方法来定义控制器。除了在您在控制器中使用它们之前尝试定义您的服务。

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'}
  });
}]);

policyServices.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 ");
});

};