这听起来很奇怪,但是我想创建一个File
无法加载的FileReader
对象。让我解释一下。
我有一个创建FileReader的函数。在该函数中,我还设置了load
和error
事件处理程序
handleFileSelect(files:ArrayLike<File>){
...
let reader:FileReader = new FileReader();
reader.onload = this.handleReaderLoaded;
reader.onerror = this.handleReaderError;
reader.readAsDataURL(file);
}
}
我想对handleFileSelect
正确设置错误处理程序以及如果handleReaderError
失败时调用错误处理程序(FileReader
)进行单元测试。但是我不知道如何使FileReader
失败。
我想到的一个思想过程是创建一个无法加载的File
,但我也无法做到这一点
到目前为止我写的规范是
fit('should call error handler when file doesn\'t get loaded successfully', (done) => {
let newPracticeQuestionComponent = component;
let file1 = new File(["foo1"], "foo1.txt");
/*
File reader will load the file asynchronously.
The `done` method of `Jasmine` makes `Jasmine` wait
When handleReaderError is called, call a fake function and within it call done
*/
spyOn(newPracticeQuestionComponent,'handleReaderError').and.callFake(function(event:FileReaderProgressEvent){
console.log("called fake implementation of handleReaderError ",event);
expect(event.type).toEqual("abort");
done();
});
newPracticeQuestionComponent.handleFileSelect([file1]);
//I SHOULD SIMULATE FILEREADER ERROR HERE BUT HOW??
});