Peter Farlow
12/21/2022, 10:26 PMclass ExampleClass(
private val loginApi: LoginApi,
externalScope: CoroutineScope,
) {
private val _loginFlow = MutableSharedFlow<String>()
val loginFlow = _loginFlow.shareIn(externalScope, SharingStarted.Lazily)
suspend fun performLogin(): String {
val result = loginApi.performLogin()
_loginFlow.emit(result)
return result
}
}
interface LoginApi {
suspend fun performLogin(): String
}
I’ve written a test case for this:
class ExampleClassTest {
private val mockApi = mockk<LoginApi> {
coEvery { performLogin() } returns "hello"
}
private val testCoroutineScheduler = UnconfinedTestDispatcher()
private val testScope = TestScope(testCoroutineScheduler)
@Test
fun `this test passes`() = runTest(testCoroutineScheduler) {
val exampleClass = ExampleClass(mockApi, testScope)
expect("hello") {
exampleClass.performLogin()
}
}
}
Can anyone help me understand why calling runTest directly on my TestScope causes this test case to fail with an eventual UncompletedCoroutinesError? The error message says the test coroutine is not completing, and also that there’s an active child job.
@Test
fun `this test times out`() = testScope.runTest {
val exampleClass = ExampleClass(mockApi, testScope)
expect("hello") {
exampleClass.performLogin()
}
}
Peter Farlow
12/21/2022, 10:27 PM