0

`I am having a hard time getting the middle? value of a stateFlow and checking using assertTrue

Here is my code for testing

fun initData() = viewModelScope.launch {
    modifiableUiState.emit(UiState.Loading)
    try {
        testSomeThing()
        modifiableUiState.emit(UiState.Loaded)
    } catch (e: Exception) {
        modifiableUiState.emit(UiState.Error(e))
    } finally {
        modifiableUiState.emit(UiState.Idle)
    }
}

and my test code

@Test
fun load_store_data_succeed() = runTest {
    // given
    var uiState: PaySettingBalanceNotificationViewModel.UiState? = null
    val collectJob = launch {
        viewModel.uiState.collect { uiState = it }
    }

    // when
    viewModel.initMandatoryData()
    runCurrent()

    // then
    assertTrue(uiState is PaySettingBalanceNotificationViewModel.UiState.InitialDataLoaded)

    collectJob.cancel()
}

since in finally block it will emit Idle, I can't check if the value has changed to InitialDataLoaded. Is there a way I can check the history? of a value for testing?`

Edric
  • 24,639
  • 13
  • 81
  • 91

0 Answers0