Angular 5 Jasmine Error: Expected one matching request for criteria found none

mrkernelpanic picture mrkernelpanic · Jun 8, 2018 · Viewed 30.2k times · Source

I have a very simple service call and a jasmine test for it.

Service call:

myServiceCall(testId: number) : void {
    const url = `${this.url}/paramX/${testId}`;
    this.http.put(url, {},{headers: this.headers}).subscribe();
}

My Test Method:

it('should call myServiceCall', inject([MyService], (service: MyService) => {
    let testId = undefined;
    service.myServiceCall(testId);
    let req = httpMock.expectOne(environment.baseUrl + "/paramX/123");

    expect(req.request.url).toBe(environment.baseUrl + "/paramX/123");
    expect(req.request.body).toEqual({});

    req.flush({});
    httpMock.verify();
}));

I get of course an exception as I expect the url parameter to be "123"and not undefined like in this test case scenario.

Error: Expected one matching request for criteria "Match URL: http://localhost:8080/myservice/paramX/123", found none.

What I don't understand is why the test framework says

found none

although the request is made. Is there any possibility to let the test framework tell me what other actual calls were made, similar to mockito's verify?

Answer

My problem is solved. After I added to params to the URL (if you use params).

let results = { param: 'results', value: '50' };
url = `${api.url}${route.url}?${results.param}=${results.value}`;

HttpTestingController always display only URL without params, but if used expectOne(url) it use a URL with query string like that: http://example.com/path/to/page?name=ferret&color=purple