微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。