I'm testing a sequence that polls a resource until a condition is met.
Book = $resource("/books/:id", {id: "@id"});
function poll(success) {
Book.get({id:1}, function() {
if (canStop) {
success();
} else {
$timeout(poll, 1000);
}
});
};
The test below fails with Error: Unsatisfied requests: GET /user_workshops/1
describe('poll', function() {
beforeEach(function() {
$httpBackend.expectGET('/books/1').respond(200,{id:1});
$httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1});
poll(function() {
successCalled = true;
});
$httpBackend.flush();
$timeout.flush();
canStop=true;
$httpBackend.flush();
});
it('should call success when canStop is true', function() {
expect(successCalled).toBe(true);
});
});
I've tried rearranging test order to put the second expectGET
just before the second httpBackend.flush()
but then I get:
Error: Unexpected request: POST /books/1
No more request expected