Android Studio 3.5.2
RxJava2
Kotlin 1.3.50
I have the following class that acts like a timer.
class DelayTimerImp(private val scheduler: IScheduler)
: DelayTimer {
override fun createTimeout(delay: Long, timeUnites: TimeUnit): Completable {
return Completable.timer(delay, TimeUnit.SECONDS, scheduler.main())
}
}
And I am using it in a method like this:
private fun startTimeoutRequest(delay: Long) {
compositeSubscription.add(delayTimer.createTimeout(delay, TimeUnit.SECONDS)
.subscribeOn(schedulers.io())
.subscribe(::timeoutHasExceeded, ::timeoutError))
}
And in my test class and it uses the TestScheduler
to return the io
thread. I am mocking the delayTimer. Even though I thenReturn(Completable.complete())
the methods in the timeOutHasExceeded
never go into them. I am thinking that if Completable
has completed in go into that method.
@Test
fun `should reload and show loading state`() {
// Arrange
whenever(delayTimer.createTimeout(10, TimeUnit.SECONDS))
.thenReturn(Completable.complete())
// Act
vipInformationPresenterImp.tryAgainTapped()
// Assert methods in the timeOutHasExceeded
}
Also If I change the following to this by changing the delay to 1
:
whenever(delayTimer.createTimeout(1, TimeUnit.SECONDS))
.thenReturn(Completable.complete())
I will get a crash
on the following line:
.subscribeOn(schedulers.io())
What is the best way to unit test this?
Many thanks in advance
TestScheduler.advanceTimeBy()
orTestScheduler.triggerActions()
? – Burn