为包含 RxJava 的 ThrottleLast 操作符的方法编写单元测试

vki*_*ins 7 android rx-java rx-java2

我有一个我想测试的方法,它看起来像这样:

 private Disposable getStuff(){
    return Observable
            .combineLatest(
                    repoOne.getStuff(),
                    repoTwo.getOtherStuff(),
                    session.getSomethingElse(),
                    session.getOtherSomethingElse(),
                    (a, b, c, d) -> new Stuff.Builder()
                            .stuff(a)
                            .stuffB(b)
                            .stuffC(c)
                            .stuffD(d)
                            .build())
            .throttleLast(500, TimeUnit.MILLISECONDS)
            .subscribeOn(mSchedulerProvider.io())
            .observeOn(mSchedulerProvider.ui())
            .subscribe(this::populateStuff, this::showError);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试此方法,但它没有通过throttleLast。我主要通过应用 a 来尝试它TestScheduler,但没有得到任何结果。例如我试过:

<...>
    .throttleLast(500, TimeUnit.MILLISECONDS,mTestScheduler)
<...>
Run Code Online (Sandbox Code Playgroud)

然后当我调用该方法时,我将时间提前:

    mCompositeDisposable.add(getStuff());
    mTestScheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    mTestScheduler.advanceTimeTo(505, TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)

但这没有任何作用,即populateStufforshowError仍然没有被调用。我可能使用TestScheduler不正确,有什么建议吗?

vki*_*ins 0

我仍然不确定为什么这不起作用,所以任何解释肯定会有帮助。但是,作为修复,我已替换throttleLastsample

.sample(500, TimeUnit.MILLISECONDS, true)
Run Code Online (Sandbox Code Playgroud)

根据我的理解throttleLast,调用时sample参数emitLast设置为 false。从逻辑上讲,无论如何我确实想发出最后一项,所以对我来说这个改变是有益的。但是,不太确定链在使用TestSchedulers 使其跳过任何发射的单元测试中的行为如何。