Reputation: 635
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').
Upvotes: 0
Views: 400
Reputation: 28368
Create a spy which you replace in the expect
:
it('should fire fireCsvDownload after exporting data and geting csv ', () => {
const mySpy = spyOn(component, 'fireCsvDownload');
component.exportToCsv(query);
fixture.detectChanges();
expect(mySpy).toHaveBeenCalled();
});
You might have to do:
const mySpy = spyOn(component, 'fireCsvDownload').and.callThrough();
But I'm not certain without testing myself.
Upvotes: 2