typescript - 如何模拟 FileReader 的失败
问题描述
我有一个创建FileReader
. 在该函数中,我还设置了load
和error
事件处理程序
handleFileSelect(files:ArrayLike<File>){
...
let reader = new FileReader()
reader.onload = this.handleReaderLoaded;
reader.onerror = this.handleReaderError;
reader.readAsDataURL(file);
}
}
我想进行单元测试以handleFileSelect
正确设置错误处理程序并且如果失败handleReaderError
则调用错误处理程序 () 。FileReader
但我不知道如何使FileReader
失败。
到目前为止我写的规范是
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??
});
解决方案
已经说过我们可以模拟readAsDataURL
方法并从中分派错误事件。但是您的 reader 是函数中的局部变量handleFileSelect
。为了访问阅读器,我们可以模拟FileReader
构造函数并控制创建的文件阅读器实例。这里我使用sinon来模拟:
// in your test:
...
// This is the reader instance that we have access
const reader = new FileReader()
// We throw an error in readAsArrayBuffer method of that instance
reader.readAsArrayBuffer = () => {
reader.dispatchEvent(new Event('error'))
}
// Now make the constructor return our instance
sinon.stub(window, 'FileReader').returns(r)
// Now make your calls and assertions
...
// Don't forget to restore the original constructor at the end
window.FileReader.restore()
推荐阅读
- sql - 从表中获取摘要结果
- flutter - Flutter如何解析JSON数据
- amazon-web-services - AWS 编码部署蓝绿部署
- javascript - PageMethod 返回完整的 html 页面
- python - Python -- Beautiful Soup -- 如果标签为空或有值,则返回信息
- reactjs - ReactJS文件上传axios
- dataframe - 如何在 PySpark 中查找非空值的列集合
- kubernetes - 尝试使用 Helm 图表安装 Kong
- javascript - JS - 拖放上传 - 限制拖动文件并允许点击拖放区打开选择文件对话框
- c# - Xamarin:将参数从绑定传递到自定义控件