简体   繁体   English

可观察测试 function call in pipe tap

[英]Observable test function call in pipe tap

I'm trying to cover this.httpCache.put(this.agreementUrl, agreement as any);我正在尝试涵盖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();
    });
});

Your service returns an observable created with this.http.您的服务返回一个使用 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.没有看到你是如何 mocking 出 http 是不可能知道发生了什么,但要调用 tap 和测试订阅,从模拟 http 返回的 observable 需要发出一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM