如何在单元测试中调用订阅错误块Angular?

How to call the subscribe error block in unit testing Angular?

我已经在 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;

试试这个:

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