RxJava Completabe然后测试

我有以下RxJava2 Kotlin代码:

val tester = Completable.complete() .andThen(SingleSource<Int> { Single.just(42) }) .test() tester.assertComplete() tester.assertValue(42) 

这模拟了一个Completable可观察(想象一个简单的更新操作到一个API),然后单个可观察(图像获取操作的API)。 我想连接两个observables的方式,当Completable完成时,Single运行,最后我在我的观察者(Int 42)上得到onSuccess事件。

但是这个测试代码不起作用。 断言失败,出现以下错误:

 java.lang.AssertionError: Not completed (latch = 1, values = 0, errors = 0, completions = 0)) 

我无法理解我在做什么错误,我期望Completable在订阅时发出onComplete,然后Single订阅,而我的观察者( tester )获得值为42的onSuccess事件,但似乎订阅保持“暂停“不发射任何东西。

这个想法与本博客文章中的想法类似: https : //android.jlelse.eu/making-your-rxjava-intentions-clearer-with-single-and-completable-f064d98d53a8

 apiClient.updateMyData(myUpdatedData) // a Completable .andThen(performOtherOperation()) // a Single<OtherResult> .subscribe(otherResult -> { // handle otherResult }, throwable -> { // handle error }); 

问题是Kotlin模糊地使用大括号:

 .andThen(SingleSource<Int> { Single.just(42) }) 

您创建了一个SingleSource ,它注意到它的SingleObserver ,但是它被Kotlin语法隐藏。 你需要的是简单的使用:

 .andThen(Single.just(42)) 

或延期使用

 .andThen(Single.defer { Single.just(42) })