19

有没有办法返回一个 HttpPromise (或类似的东西)来模拟对 的调用$http?我想设置一个全局变量来指示是否发出了真正的 HTTP 请求,或者是否返回了带有假数据的假 HttpPromise 对象。

例如,我有一个与此类似的服务:

angular
  .module('myservice')
  .factory('MyService', ['$http', function($http) {
      return {
       get : function(itemId) {
         if (isInTestingMode) {
           // return a promise obj that returns success and fake data
         }
         return $http.get("/myapp/items/" + itemId);
       }
    };
 } ]);

在我的控制器中,我调用了与上述类似的服务:

        // Somewhere in my controller

        MyService.get($scope.itemId)
           .success(function(data) {
              $scope.item = data;
           })
           .error(function(data, status, headers, config) {
              $scope.notFound = true;
           });

我试图更改控制器代码;我希望successanderror链接在我的“isInTestMode”中仍然有效。是否可以按照我在服务中描述的方式伪造HttpPromise


下面是上面的“MyService”(一个片段)的修订版,其中包含一个successerror在 promise 对象上。但是,我该如何执行该success方法?

        return {
           get : function(itemId) {
             if (isInTestingMode) {
                var promise = $.defer().promise;
                // Mimicking $http.get's success 
                promise.success = function(fn) {
                  promise.then(function() {
                     fn({ itemId : "123", name : "ItemName"}, 200, {}, {});
                  });
                  return promise;
                };
                // Mimicking $http.get's error 
                promise.error = function(fn) {
                   promise.then(null, function(response) {
                     fn("Error", 404, {}, {});
                   });
                   return promise;
                };
                return promise;
             }
             return $http.get("/myapp/items/" + itemId);
           }
        }
4

5 回答 5

18

只需使用服务的deferred方法$q

    var fakeHttpCall = function(isSuccessful) {
    
      var deferred = $q.defer()
    
      if (isSuccessful === true) {
        deferred.resolve("Successfully resolved the fake $http call")
      }
      else {
        deferred.reject("Oh no! Something went terribly wrong in your fake $http call")
      }
      
      return deferred.promise
    }

然后你可以像$httpPromise 一样调用你的函数(当然,你必须自定义你想要放入其中的任何内容)。

    fakeHttpCall(true).then(
      function (data) {
        // success callback
        console.log(data)
      },
      function (err) {
        // error callback
        console.log(err)
      })
于 2014-06-24T15:26:36.437 回答
6

我发现这篇文章与我所要求的相似。

但是,我想要一种模拟我的服务调用的方法,以便可以返回虚假数据,而不是发出真正的 HTTP 请求调用。对我来说,处理这种情况的最好方法是使用 angular 的$httpBackend服务。例如,要绕过对我的“项目”资源的 GET 请求但绕过我的部分/模板的 GET,我会执行以下操作:

angular
   .module('myApp', ['ngMockE2E'])
   .run(['$httpBackend', function($httpBackend) {
      $httpBackend
        .whenGET(/^partials\/.+/)
        .passThrough();
      $httpBackend
        .whenGET(/^\/myapp\/items\/.+/)
        .respond({itemId : "123", name : "ItemName"});
}]);

有关 $httpBackend 的更多信息,请参阅此文档

于 2014-06-24T21:46:18.070 回答
4

我终于找到了使用 jasmin 的方法$httpBackend对我来说没有选择,因为我还需要在同一服务上模拟非 $http 方法。我还认为需要指定 url 的控制器测试并不完美,因为控制器和它的测试不需要知道它。

下面是它的工作原理:

beforeEach(inject(function ($controller, $rootScope, $q) {
  scope = $rootScope.$new();
  mockSvc = {
    someFn: function () {
    },
    someHttpFn: function () {
    }
  };

  // use jasmin to fake $http promise response
  spyOn(mockSvc, 'someHttpFn').and.callFake(function () {
    return {
      success: function (callback) {
        callback({
         // some fake response
        });
      },
      then: function(callback) {
         callback({
         // some fake response, you probably would want that to be
         // the same as for success 
         });
      },
      error: function(callback){
        callback({
         // some fake response
        });             
      }
    }
  });

  MyCtrl = $controller('MyCtrl', {
    $scope: scope,
    MyActualSvc: mockSvc
  });
}));
于 2015-05-04T10:07:45.400 回答
0

十分简单!

你可以像这样使用angular-mocks-async来做到这一点:

var app = ng.module( 'mockApp', [
    'ngMockE2E',
    'ngMockE2EAsync'
]);

app.run( [ '$httpBackend', '$q', function( $httpBackend, $q ) {

    $httpBackend.whenAsync(
        'GET',
        new RegExp( 'http://api.example.com/user/.+$' )
    ).respond( function( method, url, data, config ) {

        var re = /.*\/user\/(\w+)/;
        var userId = parseInt(url.replace(re, '$1'), 10);

        var response = $q.defer();

        setTimeout( function() {

            var data = {
                userId: userId
            };
            response.resolve( [ 200, "mock response", data ] );

        }, 1000 );

        return response.promise;

    });

}]);
于 2016-09-14T20:58:09.410 回答
0

您可以实现您的 FakeHttp 类:

var FakeHttp = function (promise) {
    this.promise = promise;
    this.onSuccess = function(){};
    this.onError = function(){};
    this.premise.then(this.onSuccess, this.onError);
};
FakeHttp.prototype.success = function (callback) {
    this.onSuccess = callback;
    /**You need this to avoid calling previous tasks**/
    this.promise.$$state.pending = null;
    this.promise.then(this.onSucess, this.onError);
    return this;
};
FakeHttp.prototype.error = function (callback) {
    this.onError = callback;
    /**You need this to avoid calling previous tasks**/
    this.promise.$$state.pending = null;
    this.promise.then(this.onSuccess, this.onError);
    return this;
};

然后在您的代码中,您将从承诺中返回一个新的 fakeHttp。

if(testingMode){
    return new FakeHttp(promise);
};

承诺必须是异步的,否则将不起作用。为此,您可以使用 $timeout。

于 2016-02-16T10:25:20.550 回答