角度玩笑,测试是否在可观察范围内

时间:2021-02-11 13:23:09

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

待测试的功能

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

coverage

我走了多远

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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