Угловое тестирование - наблюдаемая труба не является функцией

Я хочу написать модульный тест для метода загрузки фотографий. Но я получаю ошибку 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);
  }));

person MarcoLe    schedule 02.10.2018    source источник
comment
опубликуйте метод snapshotChanges   -  person Sachila Ranawaka    schedule 02.10.2018
comment
Я действительно не могу ^^. Это из сторонней библиотеки, предоставленной angular-firebase. Но он возвращает Observable.   -  person MarcoLe    schedule 02.10.2018
comment
какая это версия RxJS?   -  person Nick    schedule 02.10.2018
comment
@Ник RxJs: 6.0.0   -  person MarcoLe    schedule 02.10.2018
comment
какая версия angularfire   -  person Sachila Ranawaka    schedule 02.10.2018
comment
@SachilaRanawaka angularfire2: ^5.0.0-rc.11   -  person MarcoLe    schedule 02.10.2018


Ответы (2)


Решение для модульного теста, чтобы получить работу, заключалось в добавлении этой строки: (<jasmine.Spy>service.getFileReference).and.returnValue({ getDownloadURL: () => of(null) });

person MarcoLe    schedule 02.10.2018

Насколько я понимаю, эта ошибка возникает из-за того, что 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) также должен возвращать Observable.

person Amit Vishwakarma    schedule 01.07.2020