https://kotlinlang.org logo
#rx
Title
# rx
b

blakelee

12/12/2019, 7:47 PM
I am trying to test a repeatWhen with a delay but am unable to get any results other than the first one
Copy code
@Test fun `test poll`() {
    open class MyClient {
        fun fetchItemFromServer(): Single<Boolean> = Single.just(true)
    }

    val testScheduler = TestScheduler()
    val client = mock<MyClient>()

    whenever(client.fetchItemFromServer())
            .thenReturn(Single.just(false), Single.just(true))

    val test = client.fetchItemFromServer()
            .repeatWhen { it.delay(10, TimeUnit.SECONDS, testScheduler) }
            .distinctUntilChanged()
            .test()

    testScheduler.advanceTimeBy(10, TimeUnit.SECONDS)

    test.assertResult(false, true)
}
d

Daniel Rodak

12/13/2019, 11:05 AM
advanceTimeBy
is working correctly here, the issue is in the mocks. You are getting only a first value because resubscribtion from
repeatWhen
returns
false
again which is filtered by the
distinctUntilChanged
b

blakelee

12/13/2019, 6:10 PM
Hmm… I thought maybe another advanceTimeBy would work but it’s not. Is there another way to setup mocks or do I just have to get creative?
I figured it out. Had to use a Single.defer with my values I want instead of the when statements returns false, true. Thank you for pointing me in the right direction
2 Views