我正在编写一个单元测试,以查看使用正确的 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');
});
关于javascript - toHaveBeenCalledWith 为我的单元测试用例抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56004768/