【发布时间】:2025-11-21 13:55:02
【问题描述】:
我已经在这里阅读了其他两个关于它的问题,但它们根本没有帮助
我有 3 个测试,其中我模拟了一个服务,将返回值指定为 throwError(来自 rxjs)
组件中的代码:
this.userService.resetPassword(resetReq)
.pipe(
catchError(err => {
this.setSubmittingState(form, false);
this.submitErrorMsg = 'Something wrong happend';
if (err) {
switch (true) {
case err.status === 400:
this.submitErrorMsg = 'Something in your URL might be wrong';
break;
}
}
// if no return would be set here, the first callback of the
// subscription would be performed
return throwError(err);
})
)
.subscribe(() => {
this.router.navigate([this.appRoutes.HOME]);
}, (err) => { /* whatever */ });
测试中的代码:
service.resetPassword.and.returnValue(throwError({ error: null, status: 400, message: '' }));
在 Karma 上我收到一条错误消息 Uncaught [object Object] thrown(此消息在不同的测试下抛出)
在控制台上我收到了Uncaught {error: null, status: 400, message: ""}
有人知道怎么分类吗?
【问题讨论】:
-
我们看不到
service.resetPassword在哪里被使用,所以没法说;请给minimal reproducible example。就您的测试替身而言,这是返回发出错误的可观察对象的正确方法。 -
@jonrsharpe 对我添加的内容有帮助吗?
-
return throwError(err)- 有什么可以处理重新发出的错误吗?显示错误的测试在哪里。 -
嗯,它在 pipe 中用 catchError 处理
-
不,我的意思是
throwError在那个catchError,你重新-发出它。
标签: angular karma-jasmine angular-test