samedi 7 avril 2018

Angular 5 - Angular firebase - Testing - mocking getDownloadURL method

I have difficulities in mocking the getDownLoadURL method the console everytime shows: TownhallPictureService should be return townhall level by valid input FAILED TypeError: this.ref.getDownloadURL is not a function

my Service looks like:

@Injectable()
export class TownhallPictureService {

  public ref;

  constructor(private storage: AngularFireStorage) {
  }

  getTownHallPicture(townhall: number): Observable<string | null> {
    switch (townhall) {
      case 1:
        this.ref = this.storage.ref(TownhallImgSrc.TOWNHALL_ONE);
        break;
        ...
        break;
      case 11:
        this.ref = this.storage.ref(TownhallImgSrc.TOWNHALL_ELEVEN);
        break;
      default:
        return Observable.of(undefined);
    }
    return this.ref.getDownloadURL();
  }
}

And the jasmine spec file:

    describe('TownhallPictureService', () => {
  let spy;
  const angularFireMock = {
    ref(){
      function getDownloadURL() {}
    },
  };
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [TownhallPictureService,
        {provide: AngularFireStorage, useValue: angularFireMock},
        ]
    });
    spy = TestBed.get(AngularFireStorage);
  });

  it('should be return townhall level by valid input', inject(
    [TownhallPictureService], (service: TownhallPictureService) => {
      const expectedTownhall: number = 9;
      const expectedPicture: string = TownhallImgSrc.TOWNHALL_NINE;

      spyOn(spy, 'ref').and.returnValue(Observable.of(TownhallImgSrc.TOWNHALL_NINE));

      service.getTownHallPicture(expectedTownhall).subscribe(result => {
        expect(result).toBe(expectedPicture);
      });
    }));

Does anyone know how to mock properly this method?

Aucun commentaire:

Enregistrer un commentaire