I'm new at testing, trying to take second flow value and assert it, When i run this test one by one runs fine but when i run whole test once first test runs fine and rest of test give me timeout error.
Error :
After waiting for 60000 ms, the test coroutine is not completing
kotlinx.coroutines.test.UncompletedCoroutinesError: After waiting for 60000 ms, the test coroutine is not completing
at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTestCoroutine$3$3.invokeSuspend(TestBuilders.kt:304)
(Coroutine boundary)
@OptIn(ExperimentalCoroutinesApi::class)
class HomeViewModelTest {
private lateinit var viewModel: HomeViewModel
private val testDispatcher = UnconfinedTestDispatcher()
@Before
fun setup() {
viewModel = HomeViewModel(FakeOrderRepository())
Dispatchers.setMain(testDispatcher)
}
@After
fun tearDown() {
Dispatchers.resetMain()
testDispatcher.cancel()
}
@Test
fun flowViewModelTesting1() = runTest {
val result = viewModel.homeUiState.drop(1).first()
assertThat(true).isTrue()
}
@Test
fun flowViewModelTesting2() = runTest {
val result = viewModel.homeUiState.drop(1).first()
assertThat(true).isTrue()
}
}
homeUiState
? Are you sure it is updated? – ConventhomeUiState
is always updated only once per test session, so only the first test finishes. Do you share some state/objects between instances ofHomeViewModel
that might causehomeUiState
to be updated only once, even when multipleHomeViewModel
instances are created? – Convent