javascript - toHaveBeenCalledWith 为我的单元测试用例抛出错误

标签 javascript angular

我正在编写一个单元测试,以查看使用正确的 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/

相关文章:

javascript - 如何从一个jsp页面到另一个jsp页面获取select标签中给出的值

json - 循环对象 Ionic 3/Angular 4

asp.net core 2 Angular 模板 - 在哪里添加第三方 Assets

javascript - 添加多个属性值仅适用于第一个

javascript - Javascript 对象有父子关系吗?

javascript - 根据用户代理切换 google adsense 广告(移动广告与常规广告)

javascript-show 有效,但不能隐藏

angular - 具有函数返回 Observable 和异步管道的模板绑定(bind)

angular - Angular Testing 用例中的模拟 document.querySelectorAll()

angular - 如何正确取消请求?