【问题标题】:How to call the subscribe error block in unit testing Angular? [closed]如何在单元测试Angular中调用订阅错误块? [关闭]
【发布时间】:2021-09-17 18:53:51
【问题描述】:

我已经尝试在 Angular 订阅中进行单元测试,我在测试用例下面写,但它不是 100% 到达无法访问错误块部分

组件

   this.Servive.methof(value).subscribe(res => {
   
    this.router.navigate('url');
  },
  error => {
    this.error= true;
  });

规范文件

   const spy = jest.spyOn(servive, 'method').mockReturnValue(of(appDataCreateResponse));
   component.dunction();
    expect(spy).Error(); 
    expect(component.apiError).toBeTruthy;

【问题讨论】:

    标签: angular jestjs jasmine karma-jasmine


    【解决方案1】:

    试试这个:

    import { throwError } from 'rxjs';
    ....
    const spy = jest.spyOn(servive, 'method').mockReturnValue(throwError({}));
       component.dunction();
        expect(component.error).toBe(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多