繁体   English   中英

Angular 开玩笑,测试内部是否可观察

[英]Angular jest, test if inside observable

Function待测

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

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

更改间谍,以便返回值处理这个

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM