0
Jasmine + Sinon을 사용하여 FileReader의 onload를 테스트해야합니다. 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 (비록 제대로 완료되었는지는 모르지만),하지만 스텁이 필요합니다. bed 메서드를 호출하여 FileReader의 실제 메서드 (onload)를 호출하여 테스트 할 수 있습니다.
그럴 수 있습니까?