我正在测试服务getTokenRequest是否失败。
然后我要测试这两行MyExtensions
在测试this.ngLoggerDispatcher(JSON.stringify(error));
return throwError('get token request');
中,我监视了getTokenRequest should throw an error
,并抛出了一个错误httpService.getAccessToken
然后,我致电get token request
,然后检查是否已致电utilsService.getTokenRequest
。
该测试运行成功,但是测试utilsService.ngLoggerDispatcher
失败,并显示错误getFile
,我不明白为什么。
在浏览器控制台中,我出现错误object ErrorEvent thrown
utils.service.spec.ts
Uncaught get token request
utils.service.ts
it('getTokenRequest should throw an error', inject([UtilsService, HttpService],
(utilsService: UtilsService, httpService: HttpService) => {
spyOn(httpService, 'getAccessToken').and.returnValue(throwError('get token request'));
utilsService.getTokenRequest('test.pdf', 'hashcode').pipe(
tap(() => expect(spyOn(utilsService, 'ngLoggerDispatcher')).toHaveBeenCalled())
).subscribe();
}));
it('getFile', inject([UtilsService, HttpService],
(utilsService: UtilsService, httpService: HttpService) => {
store.dispatch(new SetUrlDownload('urlDownloadTest'));
utilsService.getFile().pipe(
tap(() => expect(spyOn(httpService, 'getFile')).toHaveBeenCalledWith('urlDownloadTest'))
).subscribe();
}));
http.service.ts
getTokenRequest(fileName: string, hashcode: string): Observable<TokenRequestResponse> {
return this.httpService.getAccessToken()
.pipe(
mergeMap((accessToken: string) =>
this.httpService.getTokenRequest(fileName, hashcode, accessToken)
),
catchError((error: Error) => {
this.ngLoggerDispatcher(JSON.stringify(error));
return throwError('get file');
})
);
}
编辑:执行命令 getAccessToken(): Observable<string> {
return from(this.tokenService.token).pipe(
map(({ accessToken }) => accessToken)
);
}
getTokenRequest(fileName: string, hashcode: string, accessToken: string): Observable<TokenRequestResponse> {
return this.httpClient.post<TokenRequestResponse>('...');
}
}
我收到错误ng test --source-map=false
,但不知道有什么帮助
error screenshot