Angular 5 Jasmine 错误:预期有一个条件匹配请求未找到 [英] Angular 5 Jasmine Error: Expected one matching request for criteria found none

查看:33
本文介绍了Angular 5 Jasmine 错误:预期有一个条件匹配请求未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的服务电话和一个茉莉花测试.

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

服务电话:

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

我的测试方法:

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();
}));

我当然得到一个例外,因为我希望 url 参数是123",而不是像这个测试用例场景中那样未定义.

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

错误:预期有一个符合条件的请求匹配 URL:http://localhost:8080/myservice/paramX/123",没有找到.

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

没有找到

虽然提出了请求.是否有可能让测试框架告诉我其他实际调用是什么,类似于 mockito 的验证?

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?

推荐答案

我的问题解决了.在我将 params 添加到 URL 之后(如果您使用 params).

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 总是只显示没有参数的 URL,但如果使用 expectOne(url) 它使用带有查询字符串的 URL,如下所示:http://example.com/path/to/page?name=雪貂&color=purple

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

这篇关于Angular 5 Jasmine 错误:预期有一个条件匹配请求未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆