jean
03/14/2023, 12:52 PMawaitClose
from my unit test ? I tried to cancel testScope
but that triggers a ClosedReceiveChannelException: Channel was closed
since turbine gets also canceled.
private fun someApiCallbackToFlow(api: API) = callbackFlow {
val callback = object : ApiCallback {
override fun next(data: Int) {
trySend("Success")
}
}
api.register(callback)
awaitClose {
println("callback unregistered")
api.unregister(callback)
}
}
@OptIn(ExperimentalTime::class)
@Test
fun test() = testScope.runTest {
val api = SomeApi()
val flow = someApiCallbackToFlow(api)
flow.test {
assertEquals(1, api.callbacks.size)
api.getData()
assertEquals("Success", awaitItem())
// how can I check there isn't any callback here?
}
}
Patrick Steiger
03/15/2023, 3:03 AMtest
runTest {
coroutineScope {
launch {
flow.test {
…
cancel()
}
}
}
// verify unregister
}
runTest {
var called = false
callbackFlow<Nothing> {
awaitClose { called = true }
}.test {
cancel()
}
assertTrue(called)
}
This passesjean
03/15/2023, 8:05 AM