I'm trying to run a test but keep getting `This jo...
# coroutines
g
I'm trying to run a test but keep getting
This job has not completed yet
, any ideas what i'm doing wrong here:
Copy code
@Test
fun test() = runBlockingTest {
    val mutableStateFlow = MutableStateFlow(0)
    mutableStateFlow.value = 1
    mutableStateFlow.value = 2

    val result = mutableListOf<Int>()

    mutableStateFlow.collect {
        result.add(it)
    }

    assertEquals(listOf(0, 1, 2), result)
}
d
This test looks like it'll suspend forever. Do you have a stacktrace?
g
Instead of collect you should use take(3).toList(), otherwise it will never complete as Dominic said, StateFlow never completes
g
Oh ok, yeah that what's happening! Thanks!
d
why would it suspend forever @Dominaezzz
I’ve faced the same issue with testing retrofit suspend calls
d
`StateFlow`s never complete. Since the value can change so long as the object exists.
d
Okay thanks
Why does calling take(3).toList() make the test pass
d
take(3)
takes the first three emissions and then terminates collection.