要使用接口file[]上传的模拟文件



我正在Jasmine中编写一个测试,我想测试一个具有File[]类型参数文件的函数。

功能如下:

onAddedFile(files: File[]) {
if (files[0].type === 'text.plain') {
this.fileToSend = files[0];
this.uploadedFiles = [
{
name: files[0].name,
progress: 0,
}
];
}
}

我想在Jasmine中测试这个函数,但我不知道如何模拟文件类型file[]。

it('should add file to upload module', () => {
component.onAddedFile()
})

现在的问题是,我需要将file作为参数传递,但我收到了不同类型的错误,说参数不是file[]类型。无论我如何修改参数,它都不好。知道如何模拟一个类型正确并且可以作为文件传递的变量吗?

我尝试了以下操作,但失败了:

var file = new File([], "foo.txt", {
type: "text/plain",
});

提前谢谢。

该方法似乎需要一个文件数组,而您正在传递一个文件。

它想要File[],但你给的是File

试试这个(创建一个文件数组(:

it('should add file to upload module', () => {
const file1 = new File([], "foo.txt", {
type: "text/plain",
});
const file2 = new File([], "foo2.txt", {
type: "text/plain",
});
const files = [file1, file2];
component.onAddedFile(files);
})

最新更新