https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Melih Aksoy

06/17/2019, 1:55 PM
Hey all, I’m trying to write a test that verifies some calls are made based on what is emitted by a
flow
, but having trouble test is finished before all emits happen. Following code works all fine if I add a
delay
after
source.stateData.testObserve
Copy code
@Test
        @ExperimentalCoroutinesApi
        fun `should update state after load initial`() {
            val initialParams = mockk<PageKeyedDataSource.LoadInitialParams<Int>>(relaxed = true)
            val initalCallback = mockk<PageKeyedDataSource.LoadInitialCallback<Int, String>>(relaxed = true)

            runBlocking {

                // Fake loading
                source.loadInitial(initialParams, initalCallback)

                source.stateData.testObserve {
                    it shouldBeInstanceOf Result.State::class
                }
            }
        }
I don’t think this is the right way , how do I test a
flow
correctly ? And since we’re on it, is there a way to verify a flow is collected / collected n times etc. ?
I made
testObserve
suspending and called
suspendCoroutine
on it, and calling
resume
of
continuation
when I post values to live data, seems to solve it without a random delay / wait.
2 Views