gpt4 book ai didi

angular - 使用 retry() 和 HttpClientTestingModule 测试 HTTP 请求

转载 作者:太空狗 更新时间:2023-10-29 17:45:07 30 4
gpt4 key购买 nike

我想用 HttpClientTestingModule 测试 HTTP 调用错误响应。这工作正常,直到我将 rxjs retry(2) 添加到 HTTP 调用。然后,测试明显提示发现一个意外的请求:

Expected no open requests, found 1

但是现在,我不知道如何使用 HttpTestingController 来期待两个请求:

服务.ts

@Injectable()
export class Service {
constructor(private http: HttpClient) { }

get() {
return this.http.get<any>('URL')
.pipe(
retry(2),
catchError(error => of(null))
)
}
}

服务规范.ts

describe('Service', () => {
let httpTestingController: HttpTestingController;
let service: Service;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [Service],
imports: [HttpClientTestingModule]
});

httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(Service);
});

afterEach(() => {
httpTestingController.verify();
});

it('should handle 404 with retry', () => {
service.get().subscribe((data: any) => {
expect(data).toBe(null);
});
expectErrorResponse(404);
});

function expectErrorResponse(status: number) {
const requests = httpTestingController.match('URL');
// fails -> finds only one request
expect(requests.length).toBe(2);
requests.forEach(req => req.flush('error', {status, statusText: 'Not Found'}));
}
});

如果我删除 expect(requests.length).toBe(2),测试将失败并显示之前的错误消息。

运行示例

你可以用这个 Stackblitz 试试看

最佳答案

The fundamentals of Angular - HttpClient - retry()状态:

The RxJS library offers several retry operators that are worth exploring. The simplest is called retry() and it automatically re-subscribes to a failed Observable a specified number of times. Re-subscribing to the result of an HttpClient method call has the effect of reissuing the HTTP request.

因此,每次您调用 flush 时,它都会留下一个打开的请求。您只需要在服务重试请求时重复请求处理和刷新次数。

it('can test for 404 error', () => {
const emsg = 'deliberate 404 error';

testService.getData().subscribe(
data => fail('should have failed with the 404 error'),
(error: HttpErrorResponse) => {
expect(error.status).toEqual(404, 'status');
expect(error.error).toEqual(emsg, 'message');
}
);

const retryCount = 3;
for (var i = 0, c = retryCount + 1; i < c; i++) {
let req = httpTestingController.expectOne(testUrl);
req.flush(emsg, { status: 404, statusText: 'Not Found' });
}
});

关于angular - 使用 retry() 和 HttpClientTestingModule 测试 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439121/

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