如何触发错误并测试方法?



我正在做角度服务测试。 它工作正常。但是我无法实现100%的测试,因为误差函数没有测试。我尽力了。但无法提出 100%。有人帮我吗?

这是我的服务:

export class CandidateRegistrationManagementService {
constructor(private http: HttpClient) { }
getAvailabilityType(): Observable<ModelAvailabilityType> {
return this.http.get<ModelAvailabilityType>(environment.setUpAndConfigUrl + `LookupTypeValue/LookupValueTypeDetails?LookupTypeName=AvailabilityType`)
.pipe(
map(events => {
return events;
}),
catchError(this.handleError)
);
}

handleError(error: Response) {
return throwError(error);
}
}

我的测试用例目前我已经覆盖了 75%。 错误测试的问题。 这是我的尝试:

it('should throw error', () => {
const httpError = {
get: jest.fn(() => of(hot('#', {}, new Error('server error'))))
};
function handleError(error:any) {
return throwError(error);
}
CRMService = new CandidateRegistrationManagementService(httpError as any);
CRMService.getAvailabilityType().subscribe(data =>data, (error:any) => {
catchError(handleError(error))
})
})

但我的尝试不起作用。我没有得到100%的保险。请在此处寻求帮助。

尝试使用 -- throwError rxjs 运算符,throwError 将执行代码的错误块。

const httpError = {
get: jest.fn(() => throwError(new Error('server error'))
};

最新更新