问题描述
我有一个非常简单的服务调用和一个茉莉花测试.
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",没有找到.
我不明白的是为什么测试框架这么说
What I don't understand is why the test framework says
没有找到
尽管提出了请求.有没有可能让测试框架告诉我其他实际调用,类似于 mockito 的 verify?
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?
推荐答案
我的问题解决了.在我将参数添加到 URL 之后(如果您使用参数).
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=紫色
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 错误:预期一个匹配的条件请求未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!