如何模拟 FileReader 的失败

2024-06-26

我有一个函数可以创建一个FileReader。在该函数中我还设置了load and error事件处理程序

handleFileSelect(files:ArrayLike<File>){
...
      let reader = new FileReader()
      reader.onload = this.handleReaderLoaded;
      reader.onerror = this.handleReaderError;


      reader.readAsDataURL(file);
    }
  }

我想对其进行单元测试handleFileSelect正确设置错误处理程序并且错误处理程序(handleReaderError) 被调用如果FileReader失败了。但我不知道如何制作FileReader fail.

到目前为止我写的规范是

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??

  });

If the reader的行为正在呼唤onerror when readAsDataURL失败了,这应该做:

spyOn(newPracticeQuestionComponent.reader, 'readAsDataURL').and.callFake(() => {
    newPracticeQuestionComponent.reader.onerror();
});

由于这将作为同步调用运行,因此您可以在测试结束时简化断言(遵循三个 A),如下所示:

// Arrange
const newPracticeQuestionComponent = component;
spyOn(newPracticeQuestionComponent, 'handleReaderError');
spyOn(newPracticeQuestionComponent.reader, 'readAsDataURL').and.callFake(() => {
    newPracticeQuestionComponent.reader.onerror();
});
let file1 = new File(["foo1"], "foo1.txt");

// Act
newPracticeQuestionComponent.handleFileSelect([file1]);

// Assert
expect(newPracticeQuestionComponent.handleReaderError).toHaveBeenCalledWith({ type: 'abort' });

但我不建议期望参数传递给函数,event.type, 因为它是另一个单位的规格我们目前没有测试。 (我们正在测试newPracticeQuestionComponent不是的行为reader用事件调用错误)


嘲笑的行为reader可能不是最好的方法。这取决于您想要针对该单元进行测试的内容。

如果我们想变得极其独立,newPracticeQuestionComponent应该一无所知reader的行为甚至回调错误,该单元唯一应该知道的是设置onerror回调,你可以断言你设置了onerror读者正确。

// Arrange
const newPracticeQuestionComponent = component;
spyOn(newPracticeQuestionComponent.reader, 'readAsDataURL');
let file1 = new File(["foo1"], "foo1.txt");

// Act
newPracticeQuestionComponent.handleFileSelect([file1]);

// Assert
expect(newPracticeQuestionComponent.reader.onerror).toBe(newPracticeQuestionComponent.handleReaderError);

我不是测试方面的专家,但根据许多因素编写上面和下面的示例之类的测试似乎是有利有弊的。

希望这可以帮助 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何模拟 FileReader 的失败 的相关文章

随机推荐

  • Spark中分布式读取CSV文件

    我正在开发一个 Spark 处理框架 它读取大型 CSV 文件 将它们加载到 RDD 中 执行一些转换 最后保存一些统计数据 相关 CSV 文件平均大小约为 50GB 我正在使用 Spark 2 0 我的问题是 当我使用sparkConte
  • C++ 获取成员函数的类型(返回类型和签名),不带成员函数的“const”限定符

    是否可以在没有成员函数的 const 限定符的情况下获取成员函数的类型 返回类型和签名 到目前为止 我尝试使用 decltype T 和 std remove const std decay Example void Func std st
  • VSCode Linter ES6 ES7 Babel linter

    如何使用 Visual Studio 代码根据 babel ES7 stage 0 规则检查 JavaScript 文件 我只需要检查代码 我已经有 webpack 转译 Js 文件 我如何进行 全局安装 eslint npm instal
  • 在seaborn中使用FacetGrid为所有方面重复x轴标签

    我正在与FacetGrid给出的例子here https seaborn pydata org examples kde ridgeplot html结果如下图所示 在我的数据集中 有相当多的图 如果有x每个面都重复轴标签 而不仅仅是在底部
  • 端口尺寸与连接尺寸不匹配

    我有这个代码 Alu v module ALU src1 i src2 i src3 i src4 i ctrl i result o zero o I O ports input 32 1 0 src1 i input 32 1 0 sr
  • 在 CUDA 内核中使用虚拟函数

    所以我想在设备上分配一个具有虚拟函数的对象 然后调用内核并执行其中一些虚拟函数 我尝试了两种方法来做到这一点 但都不起作用 1 使用 cudaMalloc 和 cudaMemcpy 从主机分配和复制对象 这会复制包含主机内存指针的虚拟函数表
  • Swift 和 Cocoapods - 缺少必需的模块

    我正在尝试制作一个 Swift 框架 该框架依赖于两个库 阿拉莫菲尔 https github com Alamofire Alamofire and SwiftyJSON https github com SwiftyJSON Swift
  • Android Studio Bumblebee - 设备管理器未打开

    我刚刚将 Android Studio 更新为 Android Studio 大黄蜂 2021年1月1日 内部版本 AI 211 7628 21 2111 8092744 建于 2022 年 1 月 19 日 但现在我无法再打开设备管理器
  • 在 Selenium 中自动执行下拉菜单而不选择

    我正在尝试使用 Selenium 从下拉列表中选择一个元素 我已经能够选择下拉列表 但我不知道如何从下拉列表中选择特定元素 因为该网站不使用 select 因此我无法使用内置的 select 类 作为参考 这是下拉列表中元素之一的 HTML
  • 当执行 Tomcat 的 proxy_pass 时,Nginx 如何添加子域作为参数

    我想要实现的目标Web 应用程序应该能够支持多个子域 而无需在每次使用新子域时对 nginx 或 tomcat 进行任何更改 我已经对 DNS 进行了必要的更改以支持通配符子域 Nginx 监听端口 80 它在端口 8080 上对 tomc
  • CanExecute 何时被调用?

    在演示中 我有一个按钮可以切换布尔字段isAsking 我创建了一个命令 该命令仅在以下情况下执行isAsking true 一旦我按下切换按钮 okButton IsEnable立即更改 这表明该命令发现了更改isAsking 我感到很困
  • MVC 4 文本框未在回发时更新

    我有一个使用 modelview 对象的表单 该对象在提交表单的回发时不会更新文本框值 提交表单时 我编辑绑定到文本框的对象的属性 当表单返回时 对象属性仍然更改 但文本框值不会更改 这就像文本框值被缓存并且不会改变 我该如何解决 文本框默
  • 使关闭图像出现在 DIV 的右上角

    我想知道如何使一个小十字 闭合 图像出现在 div 的右上角 使用 CSS 和 XHTML 谢谢 你可以这样做 jsfiddle net 7JEAZ 1317 http jsfiddle net 7JEAZ 1317 代码片段 panel
  • Java 中的“实现 Runnable”与“扩展线程”

    从我什么时候开始使用线程Java 我找到了这两种编写线程的方法 With 实施Runnable public class MyRunnable implements Runnable public void run Code Started
  • UIView 的变换看起来很糟糕

    我有一个简单的视图和简单的背景 我需要旋转视图及其内容 代码在这里 CGAffineTransform r CGAffineTransformMakeRotation 5 M PI 180 0f backView transform r 我
  • 任意旋转中两条抛物线相交的代码或公式

    我正在研究一个几何问题 需要找到任何旋转中两个抛物线弧的交点 我能够通过旋转平面使弧与轴对齐来相交直线和抛物线弧 但两条抛物线不能同时与轴对齐 我正在努力推导公 式 但我想知道是否有可用的资源 我首先定义没有旋转的二维抛物线弧的方程 x t
  • Django 嵌套查询集

    我有一个像这样的 Django 数据模型 省略数据字段 class Atom Model pass class State Model atom ForeignKey Atom class Transition Model atom For
  • Safari 不会通过 http/2 加载某些资源

    服务器上启用了 Http 2 昨天我注意到在 Iphone IOS 10 2 上未加载某些资源并出现错误 failed to load resource connecting to server is not possible 当我将 Ip
  • Firebase 无法使用类检索数据[重复]

    这个问题在这里已经有答案了 我有一些功能齐全的代码行检索每个数据 但未能使用类检索它们 例如 这些线路运行良好 double value double ds child player1score getValue long value lo
  • 如何模拟 FileReader 的失败

    我有一个函数可以创建一个FileReader 在该函数中我还设置了load and error事件处理程序 handleFileSelect files ArrayLike