简体   繁体   中英

how to write unit test for service using $q and $http function in karma?

angular.module('test').service('TestServices', TestServices);
  TestServices.inject = ['$window ', '$http', ' $q'];
  function TestServices($window, $http, $q) {
  var _this = this;
  _this.getData = function() {
      var deferred = $q.defer();
      var url = '/test/url';
      //http call
      $http({ 
      method: 'GET',
      url: url 
      }).then(function(response) {
            deferred.resolve(response.data);
        }, function(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
}

Above given is my service function, i need to write unit test for service in karma. currently using angular 1.6 version. can any one know how to write the unit test???

使用$ rootScope。$ apply()它将解决所有的诺言。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM