vendredi 1 janvier 2016

AngularJS ngMock call $httpbackend.flush for nested $http

I need to write a unit test for service A, which depends on service B. Service B makes an $http calls sometimes. How can I properly call $httpBackend.flush() to get those results.

Here is the basic idea of what have:

serviceB.someMethod = function(){
  var deferred = $q.defer();
  someOtherCall()
    .then(function(data){ deferred.resolve(data) })
    .catch(function(err){
      $http.get(options).success(function(data){deferred.resolve(data)}); // <-- HTTP call
    });
  return deferred.promise;
};

serviceA = function(things, options, filterFn){
  var promises = [],
  angular.forEach(things, function(thing){
    promises.push(filterFn(thing, options)); // <-- filterFn calls serviceB.someMethod
  });
  return $q.all(promises);
};

How can I determine in my jasmine test when to call $httpBackend.flush(), so that serviceB will resolve, and thus service A? Obviously my real services have more going on, but these example should demonstrate the problem.

Aucun commentaire:

Enregistrer un commentaire