简体   繁体   English

Jest toHaveBeenCalled 无法观看被调用的方法

[英]Jest toHaveBeenCalled is not able to watch the called method

I am writing unit tests for an Angular application.我正在为 Angular 应用程序编写单元测试。 The method I am trying to test is as follows:我尝试测试的方法如下:

onUrlEntry() {
    const fileUrl = this.componentForm.get('fileUrl');
    
    if(fileUrl?.value) {
      this.URLService
      .getMetadataFromUrl(fileUrl?.value)
      .subscribe((metadataFileContent) => {
        // ...
      }, (err: HttpErrorResponse) => {
        // ...
      });
    }
}

In the spec file, I have created a stub for the fetchURLService > getMetadataFromUrl method:在规范文件中,我为 fetchURLService > getMetadataFromUrl 方法创建了一个存根:

class URLServiceStub {
  getMetadataFromUrl(url: string) {
    return undefined;
  }
}

I am providing this class inside of TestBed:我在 TestBed 中提供这个 class:

providers: [
{ provide: URLService, useClass: URLServiceStub },
]

Here's the Service:这是服务:

@Injectable({
  providedIn: 'root',
})
export class URLService{
  constructor(private httpClient: HttpClient) {}

  getMetadataFromUrl(url: string) {
    return this.httpClient.get(url, { responseType: 'text' });
  }
}

Now for the unit test:现在进行单元测试:

it('should call getMetadataFromUrl()', fakeAsync(() => {
      const mockMetadataFromUrl = 'testMetaData';

      const getMetadataFromUrlSpy = jest
        .spyOn(URLService , 'getMetadataFromUrl')
        .mockReturnValue(of(mockMetadataFromUrl));

      fixture.detectChanges();
      component.onUrlEntry();

      expect(getMetadataFromUrlSpy).toHaveBeenCalled();
}));

This is not executing.这不是执行。 Please help me out.请帮帮我。

Thanks!谢谢!

I think you need to make sure fileUrl form value has a value because most likely it does not go inside of the if check.我认为您需要确保fileUrl表单值有一个值,因为它很可能在if检查中没有 go。

Try this:尝试这个:

it('should call getMetadataFromUrl()', fakeAsync(() => {
      const mockMetadataFromUrl = 'testMetaData';

      const getMetadataFromUrlSpy = jest
        .spyOn(URLService , 'getMetadataFromUrl')
        .mockReturnValue(of(mockMetadataFromUrl));
      // !! set a value
      component.componentForm.get('fileUrl').setValue('abc');

      fixture.detectChanges();
      component.onUrlEntry();
      // !! wait for the subscription to finish (could be optional)
      tick();
      expect(getMetadataFromUrlSpy).toHaveBeenCalled();
}));

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

相关问题 调用模拟服务中的方法,但 toHaveBeenCalled() 在 Angular Jasmine 中失败 - Method in Mock service is called but the toHaveBeenCalled() fails in Angular Jasmine 如何测试是否使用 toHaveBeenCalled 调用了方法? angular - How can I test if a method was called with toHaveBeenCalled ? angular Angular 9 + jest:单元测试,模拟一个承诺并检查然后调用的方法 - Angular 9 + jest : unit test, mock a promise and check method called then JEST:如何对在类的构造函数中调用的方法进行存根 - JEST: how to stub a method which is been called in constructor of the class 如何在单元测试错误拦截器中测试 toHaveBeenCalled 的方法 - How to test a method for toHaveBeenCalled in unit testing an Error interceptor 如何验证使用 Jest 订阅后是否调用了另一个方法? TypeError:无法读取未定义的属性(读取“订阅”) - How to verify if another method was called after subscribe using Jest? TypeError: Cannot read properties of undefined (reading 'subscribe') toHaveBeenCalled()返回未定义 - toHaveBeenCalled() returns undefined 在 catchError (RxJS) 上出乎意料的 toHaveBeenCalled - Unexpected toHaveBeenCalled on catchError (RxJS) Angular:RxStomp.Watch.Subsribe 方法 - Angular: RxStomp .Watch .Subsribe Method 带有 toHaveBeenCalled 的角茉莉花测试 - angular jasmine test with toHaveBeenCalled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM