我正在测试一个轮询资源直到满足条件的序列。
Book = $resource("/books/:id", {id: "@id"});
function poll(success) {
Book.get({id:1}, function() {
if (canStop) {
success();
} else {
$timeout(poll, 1000);
}
});
};
下面的测试失败了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);
});
});
我尝试重新排列测试顺序,将第二个放在第二个expectGET
之前,httpBackend.flush()
但后来我得到:
Error: Unexpected request: POST /books/1
No more request expected