Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this simple function inside a service:

getReports(url: string): Observable<Report[]> {
    return this.http.get<Report[]>(url);
}

My test is as follows:

  it(`should call http request`, function() {
    spyOn(httpClient, 'get');
    const fakeUrl = 'https://reports.url/';
    service.getReports(fakeUrl);

    expect(httpClient.get).toHaveBeenCalledWith(fakeUrl);
    const req = httpTestingController.expectOne(fakeUrl);
  });

The first expectation passes (so httpClient.get was called with the fakeUrl, but the controller's expectOne failed:

Expected one matching request for criteria "Match URL: https://reportss.url/", found none.

Any idea why angular's testing controller doesn't recognize my call?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

After a lot of trial and error it seems that the issue stems from the fact that expectOne (and matchOne at that) only work if there's a subscriber to the http request. So this will throw an error:

httpClient.get(fakeUrl);
const request = await httpTestingController.expectOne(fakeUrl);

But this will work:

httpClient.get(fakeUrl)
   .subscribe(() => {});
const request = await httpTestingController.expectOne(fakeUrl);

This happens because the http observable is considered cold as long as there are no subscribers to it. The moment you subscribe to the observable, the observable becomes hot or open and then expectOne can find it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...