[英]How to unit test a service in angular unit testing?
I have a function in my service as follows,,我的服务中有一个功能如下,
exportPayGapDetails(filterObject: PayGapDetailFilter): void {
const url = `${this.payGapDetailExportUrls[filterObject.type]}`;
this.http
.post<PollInitResponse>(
`/adpi/rest/v2/sb/pe/v1/export/1/getStatusValue/222`,
{}
)
.subscribe(
res => {
if (res) {
this.pollingServiceService.pollRequest(
`/adpi/rest/v2/sb/pe/v1/export/1/status`,
this.ReadytoDownload.bind(this),
this.PollCondition.bind(this)
);
} else {
this.showToastMessage('error found);
}
},
() => {
this.showToastMessage('error found'
);
}
);
}
My test case,我的测试用例,
it('should call gender pay gap details init api when we pass type as GENDER_GAP on Export', fakeAsync(() => {
spyOn(pollingService, 'pollRequest').and.callThrough();
payGapDetailsService.exportPayGapDetails(filterDetailObject);
// pollingService.pollSubscriptions.unsubscribe();
tick();
const req = http.expectOne(
request =>
request.method === 'POST' &&
request.url === '/adpi/rest/v2/sb/pe/v1/export/1/getStatusValue/222'
);
req.flush(exportInitSucessResponse);
http.verify();
}));
When I run it throws me error,当我运行它时会抛出错误,
Error: Expected no open requests, found 1: GET /adpi/rest/v2/sb/pe/v1/export/1/status
I understood that it is realated to this.pollingServiceService.pollRequest( this function, but I am not sure how to resolve it.Can anyone please suggest me help.Thanks.我知道它与 this.pollingServiceService.pollRequest( 这个函数有关,但我不知道如何解决它。谁能建议我帮助。谢谢。
Try to mock this request with HttpTestingController:尝试使用 HttpTestingController 模拟此请求:
it('should call gender pay gap details init api when we pass type as GENDER_GAP on Export', fakeAsync(() => { spyOn(pollingService, 'pollRequest').and.callThrough(); payGapDetailsService.exportPayGapDetails(filterDetailObject); httpMock = TestBed.get(HttpTestingController); tick(); const request = httpMock.expectOne('/adpi/rest/v2/sb/pe/v1/export/1/getStatusValue/222'); expect(request.request.method).toEqual('POST'); req.flush(exportInitSucessResponse); http.verify(); }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.