我应该为角度控制器测试什么?

时间:2016-05-18 03:28:51

标签: angularjs karma-jasmine

我已经学会使用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');
     });
  }]);

1 个答案:

答案 0 :(得分:0)

  1. 以下代码段创建了上述服务的模拟:

    module(function($ provide){         $ provide.service(' demoService',function(){                 this.isDemoApi = jasmine.createSpy(' isDemoApi');         });     });

    //获取模拟服务的参考

    var mockDemoSvc;

    inject(function(demoService) {
        mockDemoSvc = demoService;
    });
    
  2. 要测试控制器,首先必须使用上面的代码模拟此控制器正在使用的所有服务。 这将有助于单独测试控制器API。然后,您可以继续测试与范围绑定的API,例如:toCountry()