读取文件时出现茉莉花测试用例文件错误

时间:2017-08-21 00:05:27

标签: javascript jquery html angular jasmine

  • 我正在尝试为我的代码编写单元测试用例。
  • 我在此行收到错误myReader.readAsDataURL(file);
  • 如果我在readAsDataURL方法中看到file的值,我调试了代码。
  • 我看到以下值 文件{name:“Untitled-3.txt”,lastModified:1503251185773,lastModifiedDate:Sun Aug 20 2017 13:46:25 GMT-0400(Eastern Daylight Time),webkitRelativePath:“”,size:35693,...}
  • 但不知道如何在我的单元测试用例方法中传递它。
  • 所以我收到以下错误.. TypeError:无法在'FileReader'上执行'readAsDataURL':参数1不是'Blob'类型。

  • 你能告诉我如何解决它。

  • 在下面提供我的代码和测试用例。
    testcase--->
    it('Read Financial player', (done) => {

                    console.log("Read------????");

            let callFirstTime : boolean = true;
            let url=

            spyOn(dashboardContractComponent.Financialplayers.nbcuService,'getResponse').and.
                callFake(() => { 
                        if(callFirstTime){
                            callFirstTime = false; // Invoked by detectChanges() 
                            return Observable.of([{
                                "playerId": "100",
                                "playerName": "http://localhost:3000/assets/js/actualairings.json",
                                "playerType": "TITLE",
                                "playerData": "YWZjYXJlZ2Vyamh2dmFyZWdoYnZi",
                                "notes": "",
                                "notesId": "100",
                                "elfDocID": "100",
                                "url": "http://localhost:3000/upload",
                                "date": "06/27/2017",
                                "addedByName": "Kamal",
                                "userID": "206509786",
                                "operationType": "create"
                              }, {
                                "playerId": "101",
                                "playerName": "uploadTest4.txt",
                                "playerType": "TITLE",
                                "playerData": "Manish",
                                "notes": "",
                                "notesId": "101",
                                "elfDocID": "101",
                                "url": "http://localhost:3000/upload",
                                "date": "06/27/2017",
                                "addedByName": "Kamal",
                                "userID": "206509786",
                                "operationType": "create"
                              }]
                        );
                        }
                 });

                 //const args = ['p0', 'p1', 'p2'];
                //call_me.apply(this, args);

                 //var fruits = ['Apple', 'Banana'];
                 //var fruits1 = {"test": "1"};

                  //console.log("dashboardContractComponent.Financialplayers---->" + 
                  //      JSON.stringify(dashboardContractComponent.Financialplayers.blanket.apply(this, args)));

                 spyOn(dashboardContractComponent.Financialplayers.gridkendo,'enableSaveplayer').and.returnValue(null);
                 //dashboardContractComponent.Financialplayers.fileSelect = "text.txt";
                 //dashboardContractComponent.Financialplayers.blanket(fruits[0]);
                //dashboardContractComponent.Financialplayers.blanket(fruits1.test);
                dashboardContractComponent.Financialplayers.blanket({
                    files: "Untitled-2.txt"
                });

                var myReader: FileReader = new FileReader();
                var file;
                myReader.readAsDataURL(file);
                 //dashboardContractComponent.Financialplayers.blanket( {0: File, length: 1});
                 //console.log("dashboardContractComponent.Financialplayers._dataSource._data.length---->" + dashboardContractComponent.Financialplayers._dataSource._data.length);
                 fixture.whenStable().then(() => {
               done();
               //expect(dashboardContractComponent.Financialplayers._dataSource._data.length).toEqual(3);
            });

        });

 code----------->   
     blanket(inputValue: any): void {
        var that = this;
        var file: File = inputValue.files[0];

        var myReader: FileReader = new FileReader();
        myReader.onloadend = (e) => {
          this.encodeBase64 = myReader.result;
          that.fileSelect = $("#attachplayerBrowseBtn").val().replace(/^.*\\/, "");
          if (that.fileSelect == '') {
            that.dragDrop = that.clearBtn;
          } else {
            that.dragDrop = "";
            that.dragDrop = that.fileSelect;
          }
        }
        $('.addELFplayerForm').show();
        if (inputValue.files.length > 0) {
          var fileSize = 0;

          fileSize = inputValue.files[0].size / 1048576; //size in mb 

          if (fileSize > 5) {
            alert("The player size exceeds the max limit of 5 MB");

          }

          myReader.readAsDataURL(file);
        }


  }

1 个答案:

答案 0 :(得分:0)

在浏览器中File对象实际上代表一个本地文件并包含该文件的信息。但是,当您存根该File对象时,它只有信息而不是对本地文件的实际引用。

我建议存根readAsDataURL并检查它是否在测试中收到文件对象。然后从测试中返回预定义的DataUrl。

修改

我会在你的测试中做这样的事情,

spyOn(window, 'FileReader').andReturn({
    readAsDataURL(){}
    onloadend(){}
});

你可以用jasmine stubs替换readAsDataURL()onloadend(),并断言是否用适当的参数调用它们。

修改

const fileReaderSpy = jasmine.createSpyObj('FileReader', ['readAsDataURL', 'onloadend']);

spyOn(window, 'FileReader').andReturn(fileReaderSpy);

expect(fileReaderSpy.readAsDataURL). toHaveBeenCalledWith(/* your file object */);
expect(fileReaderSpy.onloadend). toHaveBeenCalledWith(/* your callback function */);