角度测试-可观察的管道不起作用 [英] Angular testing - observable pipe is not a function
问题描述
我想为一个照片上传方法编写一个单元测试.但是出现Failed: this.task.snapshotChanges(...).pipe is not a function
TypeError: this.task.snapshotChanges(...).pipe is not a function
错误.
为了简化此问题,我将代码全部放在一个方法中:
I want to write a unit test for a photo-upload-mehtod. But I get the Failed: this.task.snapshotChanges(...).pipe is not a function
TypeError: this.task.snapshotChanges(...).pipe is not a function
Error.
For the sake of simplicity of this question, I put the code all in one method:
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));
}
Component.spec
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)
});
The solution for the unit test to get work was by adding this line:
(<jasmine.Spy>service.getFileReference).and.returnValue({
getDownloadURL: () => of(null)
});
这篇关于角度测试-可观察的管道不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!