【发布时间】:2019-07-18 19:17:03
【问题描述】:
我正在开发一个 Angular 应用程序,并且我正在使用 Jasmine 来测试该应用程序。
我想要的是用一种方法测试两个 相似 HTTP 请求,例如 ngOnInit() 。
我有一个 HTTP 请求,在 ngOnInit() 方法中调用了两次,并且
现在编写我使用的测试用例会引发如下错误:
错误:预期有一个符合条件“匹配 URL:http://localhost:8080/api/demoList”的匹配请求,但找到了 2 个请求。
例如,
// 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 的请求?
【问题讨论】:
标签: angular jasmine karma-coverage