jean
awaitClose
testScope
ClosedReceiveChannelException: Channel was closed
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
test
runTest { coroutineScope { launch { flow.test { … cancel() } } } // verify unregister }
runTest { var called = false callbackFlow<Nothing> { awaitClose { called = true } }.test { cancel() } assertTrue(called) }
A modern programming language that makes developers happier.