首页 > 解决方案 > 在 Jasmine/Karma 测试中使用 mockError 时,apollo-angular 会抛出错误

问题描述

多年来,我已经测试过此类查询/突变的错误,但就在现在,我遇到了一个错误,我似乎无法弄清楚。

当我调用这个测试时,Apollo 为我所做的 errorMock 抛出了一个错误。有没有人遇到过这个错误?

这很有趣,因为使用相同逻辑的其他测试工作得很好,只是这个

  it('should call errorHandler if getThirdPartyCompanies requests returns an error', fakeAsync(() => {
    const spyOnHandleError = spyOn(component['errorHandlerService'], 'handleError');

    component.getThirdPartyCompanies();
    const op = controller.expectOne(thirdPartyCouriersGraphqlModel);
    op.flush(
      {
        errors: [new GraphQLError('a')]
      }
    );
    controller.verify();
    tick(100);
    flush();

    expect(spyOnHandleError).toHaveBeenCalled();
  }));

图片

关于为什么会发生这种情况的任何提示?

提前致谢!

标签: angularunit-testinggraphqlapolloapollo-angular

解决方案


所以....

public getThirdPartyCompanies(): void {
      console.log('getThirdPartyCompanies');
      this.courierService.getThirdPartyCourierList(this.cityId)
                         .pipe(takeUntil(this.subscriptionDestroyer))
                         .subscribe((req) => {
        this.thirdPartyCourierList = req.data.thirdPartyCourierCompanies;
        this.loadingThirdies = false;
      }
      ), err =>{
        this.errorHandlerService.handleError(err);
        this.loadingThirdies = false;
      };
    }

这是我用于通话的代码。请注意,我在错误调用之前关闭了 observable。

我所要做的就是:

 }, err =>{
            this.errorHandlerService.handleError(err);
            this.loadingThirdies = false;
          };
        });

以正确的方式关闭订阅,我得到了它的工作......

对于任何有同样问题的人,这就是答案!


推荐阅读