Abhimanyu
09/28/2024, 5:05 PMprivate fun observeForRefreshSignal() {
viewModelScope.launch {
refreshSignal.collectLatest {
updateUiStateAndStateEvents()
}
}
}
Getting this error,
After waiting for 3s, the test coroutine is not completing, there were active child jobs:
Abhimanyu
09/28/2024, 5:08 PMprivate val testDispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(testDispatcher)
@Before
fun setUp() {
viewModel = ScreenViewModel(
coroutineScope = coroutineScope,
// other dependencies
)
}
@Test
fun `initViewModel currentAccount is null`() = runTestWithTimeout {
// ... Mocking
editAccountScreenViewModel.initViewModel()
// ... Assertions
}
private fun runTestWithTimeout(
block: suspend TestScope.() -> Unit,
) = testScope.runTest(
timeout = 3.seconds,
) {
block()
}
Abhimanyu
09/28/2024, 5:09 PMrefreshSignal.collectLatest {
updateUiStateAndStateEvents()
}
rkeazor
09/29/2024, 3:06 PMrkeazor
09/29/2024, 3:13 PM`val numberFlow = MutableStateFlow(0)
fun addElements() {
numberFlow.value = 1
numberFlow.value = 2
numberFlow.value = 3
}
@Test
fun testNumberFlow() = runTest(UnconfinedDispatcher) {
val numberList: MutableList = mutableListOf<Int>()
val listJob = launch {
numberFlow.collectLatest { numberList.add(it) }
}
addElements()
listJob.cancel()
assert ( numberList.size == 3)
assert ( numberList[0] == 1)
assert ( numberList[1] == 2)
assert ( numberList[2] == 3)
}
Dmitry Khalanskiy [JB]
09/30/2024, 7:58 AM