我404从我的 API 收到未使用的电子邮件和已使用的 204 电子邮件。现在我想使用/和将其转换为boolean(真/假)。rxjspipecatchError
emailAvailable(): Observable<boolean> {
return this.api.email(email).pipe(
map((response: HttpResponse<any>) => {
return false; // used email > false
}),
catchError((err: HttpErrorResponse) => {
if (err.status === 404) {
return of(true); // unused > true
}
throw err;
})
);
}
现在在我的单元测试中,我有这样定义的未使用电子邮件的情况
when(mockApi.email('unused@example.com')).thenThrow(
new HttpErrorResponse({status: 404, statusText: 'E-Mail not in use', error: { message: 'E-Mail not in use'}}));
然后它尝试像这样编写我的测试。
it('should validate for unused email', async() => {
expect(await readFirst(emailService.emailAvailable('unused@example.com'))).toBe(true);
});
现在测试失败了,因为HttpErrorResponse抛出了 an:
● EmailValidator › should validate form for unused email
HttpErrorResponse: Http failure response for (unknown url): 404 E-Mail not in use
所以Observable抛出了我认为我遇到的错误catchError。我的测试设置是jest,我不想(也不能)切换到 TestBed。