根据本教程,您应该使用TestScheduler覆盖它们:
class TestSchedulerProvider(private val scheduler: TestScheduler) : BaseSchedulerProvider { override fun computation() = scheduler override fun ui() = scheduler override fun io() = scheduler }
然后,本教程稍后将使用TestScheduler,如下所示:
@Test fun delayTestExample() { //given val presenter = DemoPresenter(testSchedulerProvider, view, service) given(service.getSomeRemoteData()).willReturn(Single.just(5)) val delayInMillis = 1000L //when presenter.getSomeDataWithDelay(delayInMillis) //then then(view).should(never()).showData(anyInt()) // HERE: testScheduler.advanceTimeBy(delayInMillis, TimeUnit.MILLISECONDS) then(view).should().showData(5) }
在代码的advanceTimeBy
注释下描述了的使用//HERE:
。是否可以使用相同的方法来代替您:
whenever(delayTimer.createTimeout(1, TimeUnit.SECONDS)) .thenReturn(Completable.complete())
您可以仅举一个例子:
testScheduler.advanceTimeBy(1500, TimeUnit.MILLISECONDS)
在您创建超时之后的行中,我选择了1500,以便在AdvanceTimeBy继续之前在计时器中真正勾选了1秒。尽管每个值大于1000,它都应该运行良好,我只想确定就可以了;)
根据本教程,您应该使用TestScheduler覆盖它们:
class TestSchedulerProvider(private val scheduler: TestScheduler) : BaseSchedulerProvider { override fun computation() = scheduler override fun ui() = scheduler override fun io() = scheduler }
然后,本教程稍后将使用TestScheduler,如下所示:
@Test fun delayTestExample() { //given val presenter = DemoPresenter(testSchedulerProvider, view, service) given(service.getSomeRemoteData()).willReturn(Single.just(5)) val delayInMillis = 1000L //when presenter.getSomeDataWithDelay(delayInMillis) //then then(view).should(never()).showData(anyInt()) // HERE: testScheduler.advanceTimeBy(delayInMillis, TimeUnit.MILLISECONDS) then(view).should().showData(5) }
在代码的advanceTimeBy
注释下描述了的使用//HERE:
。是否可以使用相同的方法来代替您:
whenever(delayTimer.createTimeout(1, TimeUnit.SECONDS)) .thenReturn(Completable.complete())
您可以仅举一个例子:
testScheduler.advanceTimeBy(1500, TimeUnit.MILLISECONDS)
在您创建超时之后的行中,我选择了1500,以便在AdvanceTimeBy继续之前在计时器中真正勾选了1秒。尽管每个值大于1000,它都应该运行良好,我只想确定就可以了;)