toHaveBeenCalledWith引发我的单元测试用例错误

时间:2019-05-06 11:59:00

标签: 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)
}

1 个答案:

答案 0 :(得分:0)

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