【发布时间】:2017-05-23 22:42:26
【问题描述】:
我需要使用 Jasmine + Sinon 测试 FileReader 的 onload。
这是要测试的功能:
MyObject.prototype.uploadFile = function (file, callback) {
const fileReader = new FileReader();
fileReader.onload = event => {
if (typeof callback === 'function') {
callback(event);
}
};
fileReader.readAsDataURL(file);
};
这是测试:
describe('uploadFile', () => {
it('should execute the callback', () => {
let testFunction = jasmine.createSpy();
let readData = {
readAsDataURL: () => {
this.onload();
},
onload: () => {
}
};
file = new Blob(['image']);
sandbox.stub(window, 'FileReader').returns(readData);
component = sandbox.render(BioProfile);
component.replaceImage(file, testFunction);
expect(testFunction).toHaveBeenCalled();
});
});
如您所见,我从 FileReader 存根 readData(但不确定是否正确完成),但我需要一个存根方法来调用 FileReader 的实际方法(onload)才能进行测试。
这可能吗?
【问题讨论】:
标签: testing jasmine filereader sinon