After an hour of hair pulling, I realized that httpBackend has a very specific order, which checks the called one. The wait should be set not only before you call the flash, but before requesting the resource and when you call the flash, you must make exactly and only the expected requests.
This means that if you want to reset intermediate requests, the order of requests and expectations should be accurate:
$httpBackend.expectGET('...') resource.get(); $httpBackend.flush() $httpBackend.expectGET('...') resource.get(); $httpBackend.flush() ... etc
Thus, in the case of the code above, it works if I change the order:
describe('poll', function() { beforeEach(function() { $httpBackend.expectGET('/books/1').respond(200,{id:1}); poll(function() { successCalled = true; }); $httpBackend.flush(); $httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1}); $timeout.flush(); canStop=true; $httpBackend.flush(); }); it('should call success when canStop is true', function() { expect(successCalled).toBe(true); }); });
source share