【问题标题】:Angular jest, test if inside observable角度笑话,测试是否在可观察的范围内
【发布时间】:2021-05-15 05:53:49
【问题描述】:

要测试的功能

changeIva(idTax: number, index: number): void {
    this.documentService.getTaxById(idTax).subscribe(tax => {
      if (tax.codigo === 'ISE') this.openModal(index);
    });
  }

我走了多远

it('should test whether the tax is exempt or not', () => {
    const taxMockSuccess = taxMock[0];
    const idTax = 106;
    const index = 1;
    const modalOpenSpy = spyOn(component['modal'], 'open');
    const documentServiceSpy = spyOn(documentService, 'getTaxById').and.returnValue(
      of({ taxMockSuccess })
    );

    component.changeIva(idTax, index);

    expect(documentServiceSpy).toBeCalledWith(idTax);
    expect(modalOpenSpy).toBeCalled();
  });

我需要帮助,我不知道如何在订阅中测试这个 IF

【问题讨论】:

    标签: angular jestjs angular-observable angular-unit-test


    【解决方案1】:

    您需要将tax.codigo 设置为'ISE'。这将正确地通过 IF 语句。

    更改 spy 以便返回值处理此问题

        const documentServiceSpy = spyOn(documentService, 'getTaxById').and.returnValue(
          of({ taxMockSuccess })
        );
    

    无论taxMockSuccess 是什么,它都应该将codigo 属性的值设置为'ISE'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2018-11-27
      • 1970-01-01
      • 2019-12-31
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多