How to clear AngularJS $ httpBackend answers in a different order

I have an AngularJS service that executes a $ http GET request and caches the response locally. It is designed to handle multiple calls simultaneously, so only data from the last call is cached.

In particular, if the following occurs:

  • Launch request
  • Request B started
  • Request B completed.
  • Request Completed

The result is that the response of request B is cached since it was the last initiated.

However, I had a problem testing this in Jasmine.

I can set up two wait $ httpBackend.expectGET (), but I can only clear them in the order in which they are requested.

Essentially, I need to be able to do something like this:

$httpBackend.expectGET('/one').respond(200, data1); $httpBackend.expectGET('/two').respond(200, data2); myService.doSomething('/one'); myService.doSomething('/two'); $httpBackend.flush('/two'); $httpBackend.flush('/one'); expect(myService.data).toBe(data2); 

Can anyone suggest a neat way to achieve this?

+7
javascript angularjs unit-testing jasmine
source share
1 answer

I decided to solve this by extracting HTTP calls to simple stubs. This allowed me to remove $ http from the service and $ httpBackend from unit tests and instead ridicule stubs to return promises that I could resolve in any order.

+2
source

All Articles