I am working on an Angular application and I am using Jasmine to test the application.
What I want is to test for two similar HTTP requests in one method such as, ngOnInit()
.
I have an HTTP request that is called twice in ngOnInit()
method and
write now the test case that I used throws an error like below:
Error: Expected one matching request for criteria "Match URL: http://localhost:8080/api/demoList", found 2 requests.
For example,
// 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);
});
How can I test for more than one request of similar URL?