0
Jasmine + Sinonを使用してFileReaderのオンロードをテストする必要があります。jasmineで実際のメソッドを呼び出すメソッドをスタブする
これをテストするための機能です:
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();
});
});
あなたが見ることができるように、私はからて、readDataをスタブFileReader(ただし、正常に実行されたかどうかはわかりませんが)、スタブが必要ですFileReaderの実際のメソッド(onload)を呼び出してテストすることができます。
これは可能ですか?