代码之家  ›  专栏  ›  技术社区  ›  MarcoLe

角度测试-可观察管道不是功能

  •  1
  • MarcoLe  · 技术社区  · 6 年前

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

      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);
      }));
    
    1 回复  |  直到 6 年前
        1
  •  7
  •   MarcoLe    6 年前

    单元测试获得工作的解决方案是添加以下行: (<jasmine.Spy>service.getFileReference).and.returnValue({ getDownloadURL: () => of(null) });

        2
  •  1
  •   Leogout    4 年前

    据我所知,这个错误是因为 this.task.snapshotChanges(...) 返回一个 Object 在间谍中。 相反,它应该返回一个 Observable .

    const spy = (<jasmine.Spy>serviceStub.uploadPhoto).and.returnValue({
      percentageChanges: () => of(null),
      snapshotChanges: () => {
        return of({
          getDownloadURL() {
            return of(null);
          }
        })
      }
    

    });

    也, getDownloadURL: () => of(null)