简体   繁体   English

如何对 rxjs shareReplay 进行单元测试?

[英]How to unit test rxjs shareReplay?

I am trying to test the following scenario for a service using RxJS's shareReplay method which caches the response so that subsequent subscriptions do not need to make another call:我正在尝试使用 RxJS 的shareReplay方法测试服务的以下场景,该方法缓存响应,以便后续订阅不需要再次调用:

it('should only make 1 http call for multiple subscriptions', () => {

  myService.doSomeAsyncOperation('ABCD').subscribe();
  myService.doSomeAsyncOperation('EFGH').subscribe();
  myService.doSomeAsyncOperation('IJKL').subscribe();

  // Expectation?

});

It would seem that exposing some form of observable state is necessary to test this properly, eg .getCallsMade() which could be used to say:似乎暴露某种形式的可观察状态对于正确测试是必要的,例如.getCallsMade()可以用来说:

expect(myService.doSomeAsyncOperation('IJKL').subscribe().getHttpCallsMade() < 2).toBeTruthy()

I could add a counter and try to do this manually, but this doesn't seem optimal as it's quite a bit of state to manage.我可以添加一个计数器并尝试手动执行此操作,但这似乎不是最佳选择,因为要管理的状态相当多。

Any ideas?有任何想法吗?

Seems this is the answer:似乎这是答案:

httpTestingController.expectOne('endpoint-called-from-async-operation');

https://angular.io/api/common/http/testing/HttpTestingController#expectone https://angular.io/api/common/http/testing/HttpTestingController#expectone

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

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