[英]toHaveBeenCalledWith throws error for my unit test case
我正在编写一个单元测试,以查看使用正确的URL调用该服务。 PFB我的测试用例和代码。 但我得到这个错误:
ContactTriageService › it should get data from ContactInfo endpoint based
on the url inputted
expect(jest.fn())[.not].toHaveBeenCalledWith()
Matcher error: received value must be a mock or spy function
Received has type: string
Received has value: "contact-info"
// 测试用例
it('it should get data from ContactInfo endpoint based on the url inputted', () => {
const url ='contact-info';
const { service, get } = spectator;
const http = get<HttpClient>(HttpClient).get.and.returnValue(of());
service.getserviceResponse(url).subscribe();
expect(http.calls.first().args[0]).toHaveBeenCalledWith(url);
});
// service.ts
getserviceResponse(url: string): Observable<ContactInfoSelectionResponse|ContactInfoResponse> {
return this.http.get<any>(url)
}
it('should call the contact info endpoint', () => {
const { service, get } = spectator;
const http = get<HttpClient>(HttpClient).get.mockReturnValue(of());
service.getContactInfoData().subscribe();
expect(http.mock.calls[0][0]).toEqual('/contact-info');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.