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

六月ゝ 毕业季﹏ 提交于 2020-01-01 09:48:28

问题


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?


回答1:


My problem solved. after I added to url, params(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 url with query string like that: http://example.com/path/to/page?name=ferret&color=purple




回答2:


You've read the error wrong, let me rephrase it for you :

Error: Expected one matching request [...], found none.

This simply means that your URL doesn't match.

What you can do is add a console log of your URL with

console.log(req.request.url);

Or you can simply try to match the request.

Other solution : since you rely on environment variables, you can run this test instead :

expect(req.request.url.endsWith("/paramX/123")).toEqual(true);



回答3:


You should have your test inside a fakeAsync and call tick() at the end of your test. like

it('should call myServiceCall', inject([MyService], fakeAsync((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();
    tick();

})));


来源:https://stackoverflow.com/questions/50755939/angular-5-jasmine-error-expected-one-matching-request-for-criteria-found-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!