首页 > 解决方案 > 为什么 `blockingGet()` 有效,但 `assertValue()` 在 RxJava 中写入然后读取 DB 时不起作用?

问题描述

我正在运行一个测试,我运行一个写,然后读操作:

emailRepository.insertEmail(email); // Returns a Completable
emailRepository.getEmail(email); // returns a Maybe<String>

如果我尝试使用TestObserver<String>以下方法对其进行测试,我会得到一个空MaybegetEmail()并且测试失败:

emailRepository.insertEmail(email)
  .andThen(Maybe.defer(() -> emailRepository.getEmail(email)))
  .test()
  .assertValue(email);   // fails -- empty Maybe

但是,如果我使用 运行它blockingGet(),则相同的测试有效:

String emailFromDb = emailRepository.insertEmail(email)
  .andThen(Maybe.defer(() -> emailRepository.getEmail(email)))
  .blockingGet();

assertValue(email,emailFromDb);    // succeeds

我认为通过链接该test()方法可以降低竞争条件的风险,但奇怪的是,情况似乎并非如此。

这可能只是一个巧合blockingGet()吗?我的理解是,一旦将任务移交给数据库(在本例中为 MySql),Completable就会完成,但数据库可能还没有完成写入。但如果是这样的话,那么我不应该看到它有时也会失败blockingGet()吗?还是我在这里误解了一些东西?

标签: javarx-java

解决方案


推荐阅读