【发布时间】:2019-01-15 13:27:13
【问题描述】:
寻找最佳实践如何在 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();
});
有没有更好的方法来使用 TypeScript 设置文件大小属性?
当我尝试将size 属性直接设置为 File 对象时,这是不允许的,因为size 属性是read-only。
const file = new File([''], 'filename', { type: 'image/png' });
file.size = 8242880; // TS error
目前我如何模拟需要为我定义所有文件对象属性的文件对象的方式看起来不是很漂亮,但找不到更好的方法。 有什么想法吗?
【问题讨论】:
标签: angular file typescript unit-testing jasmine