[英]Test observable evaluated only once
We are using rx-java, but we have a problem to test if an Observable is evaluated only once. 我们正在使用rx-java,但是测试Observable是否仅被评估一次存在一个问题。
We have the following signature of services: 我们具有以下服务签名:
class A {
Observable<String> computeA();
}
class B {
Observable<Void> computeB(String inputA)
}
class C {
Observable<Void> computeC(String inputA)
}
In our case we have a service that invokes a service A, and we use the result of this service as input to another two service B and C. We want to check that the service A in only invoked once. 在我们的例子中,我们有一个调用服务A的服务,并将该服务的结果用作对另外两个服务B和C的输入。我们要检查服务A仅被调用一次。
We tried to do it using AtomicInteger as a counter mocking the Observable of service A or just mocking Observable.OnSubscribe checking the times call is invoked, but the test turns very difficult to read. 我们尝试使用AtomicInteger作为模拟服务A的Observable的计数器或仅模拟Observable.OnSubscribe来检查调用调用的时间来进行此操作,但是该测试变得很难读取。
The question is: Is there any cleaner way to do this kind of test? 问题是:有没有更清洁的方法来进行这种测试?
We usually do this by adding an 我们通常通过添加一个
source
.doOnSubscribe(() -> counter.getAndIncrement())
.op()
.op()
...
at the required place and check the counter value after things quiet down. 在所需的地方,待事情平静下来后再检查计数器值。
Note that we have an operator called publish()
that makes sure a source is subscribed to once and all side-effects of it happen only once (as long as connect() is called once). 请注意,我们有一个称为publish()
的运算符,该运算符可确保对源进行一次订阅,并且其所有副作用仅发生一次(只要connect()被调用一次)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.