How to flush AngularJS $httpBackend responses in different order
Asked Answered
C

1

7

I have an AngularJS service which performs an $http GET request and caches the response locally. It is designed to handle the multiple calls happening simultaneously, such that only the data from the final call is cached.

Specifically, if the following happens:

  • Request A Started
  • Request B Started
  • Request B Completed
  • Request A Completed

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

However I'm having trouble unit testing this in Jasmine.

I can set-up two $httpBackend.expectGET() expectations, but I can only flush them in the order they are requested.

Essentially I need to be able to so 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?

Cowbane answered 3/2, 2015 at 18:8 Comment(2)
Maybe you can play with the count property docs.angularjs.org/api/ngMock/service/$httpBackend#flush but I don't think it will be enoughTureen
Unfortunately the documentation specifically says about the count property that it specifies the "Number of responses to flush (in the order they arrived)". I suspect I'll need to do something clever with spies to change the behaviour of $http or $httpBackend in some way.Cowbane
C
2

I ended up solving this by extracting out the HTTP calls into simple stubs. That allowed me to remove $http from the service and $httpBackend from the unit tests, and instead mock the stubs to return promises that I could resolve in any order.

Cowbane answered 27/2, 2015 at 12:11 Comment(1)
I'm interested in a more detailed version of your solution : could you share a code snippet ?Etty

© 2022 - 2024 — McMap. All rights reserved.