gpt4 book ai didi

angular-resource - 多个顺序的httpBackend请求导致意外的请求

转载 作者:行者123 更新时间:2023-12-04 17:08:39 24 4
gpt4 key购买 nike

我正在测试轮询资源直到满足条件的序列。

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

最佳答案

经过一个小时的梳理,我意识到httpBackend是,非常,具体取决于测试被调用的顺序-必须不仅在调用flush之前,而且在发出资源请求之前以及调用flush时,都必须设置期望值您必须完全准确且仅提出了预期的要求。

这意味着,如果要在顺序请求之间进行刷新,则请求的顺序和期望必须完全相同:

$httpBackend.expectGET('...')
resource.get();
$httpBackend.flush()
$httpBackend.expectGET('...')
resource.get();
$httpBackend.flush()
...
etc

因此,在上面的代码中,如果我将顺序更改为:
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);
});
});

关于angular-resource - 多个顺序的httpBackend请求导致意外的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com