jeudi 26 septembre 2019

How to test an observable stream?

I want to write unit tests in Angular for the following function:

exportToCsv(query: string) {
    this.exportToCsvService.exportDataAndGetCsv(query).pipe(
      first(),
      tap(response => this.fireCsvDownload(response))
    ).subscribe();
  }

The function exportDataAndGetCsv makes a http call and returns a string. I think the test I would write should check if the fireCsvDownload was executed. I tried:

it('should fire fireCsvDownload after exporting data and geting csv ', () => {
    component.exportToCsv(query);
    fixture.detectChanges();
    expect(component.fireCsvDownload).toHaveBeenCalled();
  });

But I get an error: Error: <toHaveBeenCalled> : Expected a spy, but got Function. What should I do? I provide the exportToCsv service to the TestBed and the exportDataAndGetCsv returns of('text').

Aucun commentaire:

Enregistrer un commentaire