简体   繁体   English

测试可观察性仅评估一次

[英]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.

相关问题 不明白“除了E1只评估一次”之外的含义 - Do not understand the meaning of “except that E1 is evaluated only once” for 循环的“计数限制”表达式是只计算一次,还是每次迭代? - Will the "count limit" expression of a for-loop be evaluated only once, or on each iteration? Android RX - Observable.timer只触发一次 - Android RX - Observable.timer only firing once 在java增强的for循环中,可以安全地假设要循环的表达式只会被评估一次吗? - In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once? 在java增强的for循环中,可以安全地假设要循环的表达式只会被评估一次吗? - In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once? @after @before仅使用一次,几次@test - @after @before used only once, several @test 如何在spring测试中的@Test方法之前只填充一次数据库? - How to populate database only once before @Test methods in spring test? Spring + RxJava + Schedule cron作业:为什么bean在可连接的可观察对象上仅被调用一次 - Spring + RxJava + Schedule cron job: why beans called only once on connectable observable JUNIT:为大量测试类只运行一次安装程序 - JUNIT : run setup only once for a large number of test classes 对多个测试类仅执行一次初始化代码 - Executing Initialization Code only once for multiple test classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM