Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush AngularJS $httpBackend responses in different order

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?

like image 510
James Thurley Avatar asked Feb 03 '15 18:02

James Thurley


1 Answers

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.

like image 178
James Thurley Avatar answered Sep 23 '22 14:09

James Thurley