Angular 测试:模拟抛出错误的服务会使其他测试失败

问题描述

我已经在这里阅读了另外两个关于它的问题,但它们根本没有帮助 我有 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,message: ""}

有人知道怎么排序吗?

解决方法

我修复了它在组件中返回 empty() 而不是 throwError(error)