gpt4 book ai didi

Angular Testing - 可观察管道不是函数

转载 作者:行者123 更新时间:2023-12-02 08:04:58 25 4
gpt4 key购买 nike

我想为照片上传方法编写一个单元测试。但我得到 Failed: this.task.snapshotChanges(...).pipe is not a function
TypeError: this.task.snapshotChanges(...).pipe is not a function
错误。

为了这个问题的简单起见,我将代码全部放在一个方法中:

组件

  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));
}

组件.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)
});

关于 Angular Testing - 可观察管道不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52607037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com