2016-05-18 59 views
0

我學習了使用Karma來測試我的angularjs應用程序。但是,我的控制器中不少使用多個服務,這些服務是檢索json然後加載到頁面的http請求。我一直因爲兩個問題而無法回答。 1)如何模擬這些服務2)我應該爲我的控制器測試什麼問題2我覺得很難回答,因爲控制器功能取決於我在其他控制器中使用的服務。無論如何,讓我告訴我的控制器之一,然後,「庫」房我的服務:我應該測試我的角度控制器?

我的一個控制器的

angular.module('ccApp') 
.controller('CountriesCtrl', ['$scope', '$routeParams',    '$location','countryInfo', 'getCountries', 'countriesCache', 'getNeighbors', 
    'buildCountry', '$timeout', '$q', 
    function($scope, $routeParams, $location, countryInfo, getCountries,  countriesCache, getNeighbors, 
    buildCountry, $timeout, $q){    
    getCountries.countriesObject.then(function(response){ 
     $scope.geocountries = response.data.geonames; 
    }, 
    function(response){ 
     alert("error"); 
    }); 

    $scope.toCountry = function(geocountry){ 
     getNeighbors(geocountry.geonameId) 
     .then(function(response){ 
      buildCountry(geocountry, response); 
      var path = '/countries/'+countryInfo.name+'/capital'; 
      $location.path(path); 
     }), 
     function(response){ 
      alert('Error'); 
     }; 
    }; 
    $scope.goHome = function(){ 
     $location.path('/'); 
    }; 

}]);

我應該在控制器規格中測試什麼?

這裏就是服務收納在庫:

angular.module('library', []) 
.service('countryInfo', function(){ 
    var country = { 
     name: '', 
     pop: '', 
     area: '', 
     capital: '', 
     code: '', 
     capPop: '', 
     numNeigh: 0, 
     neighbors: [] 
    }; 
    return country; 
}) 
.factory('countriesCache', ['$cacheFactory', function($cacheFactory){ 
    return $cacheFactory('countriesCached'); 
    }]) 
.factory('getCountries', ['$http', function($http){ 
    var request = { 
      username: 'vman' 
     }; 
    return { countriesObject : $http({ 
      method: 'GET', 
      url: 'http://api.geonames.org/countryInfoJSON', 
      params: request 
     })}; 
    }]) 
    .factory('getCountry', ['$http', function($http){ 
    return function(countryCode){ 
     return $http({ 
      method: 'GET', 
      url: 'http://api.geonames.org/countryInfoJSON', 
      params: { username: 'vman', country: countryCode } 
     }); 
    }; 
    }]) 
    .factory('getNeighbors', ['$http', function($http){ 
    return function(geonameId){ 
     return $http({ 
      method: 'GET', 
      url: 'http://api.geonames.org/neighboursJSON', 
      params: { username: 'vman', geonameId: geonameId } 
     }); 
    }; 
    }]) 
    .factory('buildCountry', ['countryInfo', '$q', '$timeout',  function(countryInfo, $q, $timeout){ 
    return function(geocountry, response){ 
     countryInfo.name = geocountry.countryName; 
     countryInfo.code = geocountry.countryCode; 
     countryInfo.pop = geocountry.population; 
     countryInfo.area = geocountry.areaInSqKm; 
     countryInfo.capital = geocountry.capital; 
     countryInfo.neighbors = response.data.geonames; 
     countryInfo.numNeigh = response.data.geonames.length; 
    }; 
    }]) 
    .run(['$rootScope', '$location', function($rootScope, $location) { 
     $rootScope.$on('$routeChangeError', function() { 
     $location.path('/error'); 
    }); 
    }]); 

回答

0
  1. 下面的片斷創建上述服務的模擬:

    模塊(函數($提供){ $提供。服務('demoService',函數(){ this.isDemoApi = jasmine.createSpy('isDemoApi'); }); });

    //獲取嘲笑服務的參考

    變種mockDemoSvc;

    inject(function(demoService) { 
        mockDemoSvc = demoService; 
    }); 
    
  2. 檢測控制器,首先你必須用上面的代碼來嘲笑此控制器使用的所有服務。 這將有助於分開測試控制器API。然後你就可以繼續前進,以測試其綁定與範圍的API,如:toCountry()

+0

@suneet_bansal感謝鋪設了詳細的模擬。我會在稍後嘗試。所以,我是否正確理解你,我沒有測試實際服務,而是調用服務的範圍函數?更具體地說,我測試的範圍函數調用服務,在這種情況下,純粹模擬服務 – Vman

+0

是的,你砰的一聲。 –

相關問題