问题
I have imported file-saver in angular 5 application, when I am executing test cases it is giving following error:
TypeError: FileSaver.saveAs is not a function
Spec.ts:
import FileSaver from 'file-saver';
.ts
import FileSaver from 'file-saver';
FileSaver.saveAs(blob, filename);
How to mock filesaver in test cases.
回答1:
This works in Angular 7, so I'm guessing it will work in NG5. The first thing is to import FileSaver using the 'as' syntax in both the component and the spec:
import * as FileSaver from 'file-saver';
Then in the spec beforeEach:
spyOn(FileSaver, 'saveAs').and.stub();
来源:https://stackoverflow.com/questions/49479501/testing-filesaver-in-angular-5