小编Tom*_*ais的帖子

使用 Angular 中的 TypeScript 对文件大小进行 Jasmine 单元测试

搜索最佳实践如何在 input type="file" 更改事件上测试文件大小。

现在我的测试规范是这样的:

it('attach file with too large size', () => {
  const file: File = {
    name: 'filename',
    type: 'image/png',
    size: 8242880,
    lastModified: 1,
    lastModifiedDate: new Date(),
    webkitRelativePath: '',
    msClose: () => {},
    msDetachStream: () => {},
    slice: (): Blob => null,
  };
  const event = { target: { files: [file] } };

  component.onFileChange(event); // file validation happens into it too

  const error = control.getError('file_size');
  expect(error).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法如何使用 TypeScript 设置文件大小属性?当我尝试将size属性直接设置为 File 对象时,这是不允许的,因为size属性是read-only …

unit-testing file jasmine typescript angular

8
推荐指数
1
解决办法
3769
查看次数

在 Jasmine + Angular 中模拟“窗口”对象

我有一个要进行单元测试的函数,我正在比较全局对象window& parentas const isEqual = (window === parent);

在 Angular/TypeScript 中模拟这些对象的最佳方法是什么?

另一个想法是通过函数参数获取这些对象,但无论如何它都不能解决这个问题,因为window如果我使用,我也需要模拟全局对象getSomeData(win: Window, parent: Window) { // ... }

unit-testing jasmine typescript angular

7
推荐指数
1
解决办法
6776
查看次数

标签 统计

angular ×2

jasmine ×2

typescript ×2

unit-testing ×2

file ×1