【发布时间】:2019-01-28 22:34:41
【问题描述】:
我正在尝试 100% 的测试覆盖率,但我似乎无法测试这个 onUploadFile 函数中的内容。
html 模板
<input type="file" formControlName="theUpload" id="upload" (change)="onUploadFile($event, i, true)">
filing.ts 文件
onUploadFile(evt: Event, index: number, isReq: boolean = false): void {
const reader = new FileReader();
const target = <HTMLInputElement>evt.target;
if (target.files && target.files.length) {
const file = target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.getUploadFormGroup(index, isReq).patchValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1],
dateUploaded: new Date()
});
console.log(
`getUploadFormArray (${isReq ? 'required' : 'other'} forms)`,
this.getUploadFormArray(isReq)
);
};
}
}
filing.spec.ts 文件
it('should call onUploadFile when input is changed for required files', () => {
spyOn(component, 'onUploadFile').and.callThrough();
const fakeChangeEvent = new Event('change');
const targ = <HTMLInputElement>de.nativeElement.querySelector('input#upload');
targ.dispatchEvent(fakeChangeEvent);
fixture.whenStable().then(() => {
expect(component.onUploadFile).toHaveBeenCalledWith(fakeChangeEvent, 0, true);
expect(targ.files.length).toBeGreaterThan(0); // this is always zero because i can't add to targ.files (readonly FileList)
});
});
我愿意模拟任何可以模拟的东西,但有人可以告诉我如何测试console.log 函数(要求我在target.files 数组中至少有一项?
【问题讨论】:
标签: angular typescript unit-testing jasmine