簡體   English   中英

單元測試 Angular 5 應用程序的模擬文件列表

[英]Mock FileList for Unit Test Angular 5 App

模擬文件清單

我正在嘗試編寫一個需要 FileList 的單元測試 (Angular5)。 我到處尋找解決方案的任何提示。 我想知道這是否可能,因為 FileList 的安全性,我的追求從一開始就注定了。

如果這是可能的,任何指針將不勝感激。

選項 1:使用 DataTransfer 構造函數

describe('Component', () => {
  const getFileList = () => {
    const dt = new DataTransfer();
    dt.items.add(new File([], 'file.csv'));
    return dt.files;
  };

  it('should mock fileList', () => {
    component.fileList = getFileList();
  });
});

選項 2:使用 Blob 模擬文件列表

describe('Component', () => {
  const getFileList = () => {
    const blob = new Blob([""], { type: "text/html" });
    blob["lastModifiedDate"] = "";
    blob["name"] = "filename";
    const file = <File>blob;
    const fileList: FileList = {
      0: file,
      1: file,
      length: 2,
      item: (index: number) => file
    };
    return fileList;
  };

  it('should mock fileList', () => {
    component.fileList = getFileList();
  });
});

快樂編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM