RxJava Completabe然后测试

Mig*_*ran 9 rx-java

我有以下RxJava2 Kotlin代码:

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

这模拟了一个Completable observable(想象一个对API的简单更新操作),然后是一个Single observable(在API上映像获取操作).我希望以一种方式连接两个observable,当Completable完成时,Single运行,最后我在我的观察者(Int 42)上获得onSuccess事件.

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

java.lang.AssertionError: Not completed
(latch = 1, values = 0, errors = 0, completions = 0))
Run Code Online (Sandbox Code Playgroud)

我无法理解我做错了什么,我希望Completable在订阅时发出onComplete,然后Single订阅,我的observer(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
    });
Run Code Online (Sandbox Code Playgroud)

aka*_*okd 16

问题是Kotlin对花括号的模糊使用:

.andThen(SingleSource<Int> { Single.just(42) })
Run Code Online (Sandbox Code Playgroud)

你创建了一个SingleSource注意到它SingleObserver,但它被Kotlin语法隐藏.你需要的是明确的用途:

.andThen(Single.just(42))
Run Code Online (Sandbox Code Playgroud)

或延期使用

.andThen(Single.defer { Single.just(42) })
Run Code Online (Sandbox Code Playgroud)