【发布时间】:2022-01-04 14:37:49
【问题描述】:
以下是我的服务文件中的 2 个方法
public getStates(countryId: number) {
let url = this.configManagerService.statesApiUrl;
const httpHeaders = this.getHeaders();
return this.httpClient.get(`${url}/${countryId}`, { headers: httpHeaders }).pipe(
catchError(this.handleError)
);
}
public handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(`Backend returned code ${error.status}, ` + `body was: ${error.error}`);
}
// return an observable with a user-facing error message
return throwError('Value list currently not available.');
}
我无法编写涵盖handleError 方法的单元测试用例。有人可以教育我吗?谢谢。
【问题讨论】: