我想为照片上传方法编写一个单元测试。但我明白了Failed: this.task.snapshotChanges(...).pipe is not a function
TypeError: this.task.snapshotChanges(...).pipe is not a function
Error.
为了简单起见,我将代码全部放在一个方法中:
成分
public startUpload(event: FileList) {
const file: File = event.item(0);
const pathRef = `users/${this.uid}`;
this.task = this.service.uploadPhoto(pathRef, file);
this.fileRef = this.service.getFileReference(pathRef);
this.percentage = this.task.percentageChanges();
this.snapshot = this.task.snapshotChanges();
this.task.snapshotChanges().pipe(last(), switchMap(() => // it fails here - need to propperly mock this
this.fileRef.getDownloadURL()))
.subscribe(url => this.service.updatePhoto(url));
}
组件规格
it('should upload file', async(() => {
const supportedFile = new File([''], 'filename.png', {type: 'image/', lastModified: 2233});
const fileList = {
item: () => {
return supportedFile;
}
};
const spy = (<jasmine.Spy>serviceStub.uploadPhoto).and.returnValue({
percentageChanges: () => of(null),
snapshotChanges: () => {
return {
getDownloadURL() {
return of(null);
}
};
}
});
component.startUpload(<any>fileList);
expect(spy).toHaveBeenCalledWith(`users/${component.uid}`, supportedFile);
}));
单元测试正常工作的解决方案是添加以下行:(<jasmine.Spy>service.getFileReference).and.returnValue({
getDownloadURL: () => of(null)
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)