我正在开发一个 Angular 应用程序,我正在使用 Jasmine 来测试该应用程序。
我想要的是测试两个 类似 一种方法中的 HTTP 请求,例如 ngOnInit()
.
我有一个在 ngOnInit()
中被调用两次的 HTTP 请求方法和
现在编写我使用的测试用例会引发如下错误:
Error: Expected one matching request for criteria "Match URL: http://localhost:8080/api/demoList", found 2 requests.
例如,
// method to test
ngOnInit() {
this.httpGetRequest();
// some other code
this.httpGetRequest();
}
this.httpGetRequest() {
this.httpClient.get(http://localhost:8080/api/getSomeList);
}
//test case for ngOnInit()
it('should do something', () => {
spyOn(component, 'ngOnInit').and.callThrough();
component.ngOnInit();
const req = httpTestingController.expectOne(`http://localhost:8080/api/getSomeList`);
expect(req.request.method).toEqual('GET');
req.flush(mockList);
});
如何测试多个相似 URL 的请求?
最佳答案
您可以使用 match
方法而不是 expectOne
.在您的情况下,它应该返回一个包含 2 个元素的数组,每个类似请求一个。
关于angular - 预期 1 个匹配请求,发现 2 个请求。我如何测试 2 个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865297/