5

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Saurabh Gupta
  • 315
  • 1
  • 5
  • 14

1 Answers1

5

You can use the match method instead of expectOne. In your case it should return an array with 2 elements, one for each of the similar requests.

Eric Simonton
  • 5,702
  • 2
  • 37
  • 54