0

I'm trying to cover this.httpCache.put(this.agreementUrl, agreement as any); line in test but it always fails,

public getTransportAgreement(): Observable<TransportAgreement> {
        const cache: TransportAgreement = this.httpCache.get(
            this.agreementUrl
        ) as unknown as TransportAgreement;
        Iif (cache) {
            return of(cache);
        }
        return this.http.get(this.agreementUrl).pipe(
            tap((agreement: TransportAgreement) => {
                this.httpCache.put(this.agreementUrl, agreement as any);
            })
        );
    }

Test:

fit('can get put transport agreement cache', () => {
    spectatorHttp.service.getTransportAgreement().subscribe(() => {
        expect(spectatorHttpCache.service.put).toHaveBeenCalled();
    });
});

1 Answers1

0

Your service returns an observable created with this.http. Without seeing how you are mocking out http it is impossible to know what is going on but for the tap and the test subscription to be called the observable returned from the mock http needs to emit something.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60