所以我收到以下错误.. 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);
}
}
答案 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 */);