Tgo1014
09/05/2023, 12:41 PMViewModel
that has a viewModelScope.launch{}
on the init{}
. I’m trying to run a test where I set Dispatchers.setMain(StandardTestDispatcher())
but the launch{}
is running before I call advanceUntilIdle()
. Why is that? Can someone give me a light 😅Stylianos Gakis
09/05/2023, 7:06 PMsetMain
early enough?
Also, I know it might not be super helpful to you now, but could you move that out of the init
in the first place 😅 Every time I’ve done that I realized just how brittle that entire thing is and have ended up re-writting it in a much better way instead.Tgo1014
09/05/2023, 7:45 PMuli
09/06/2023, 8:32 AMStylianos Gakis
09/06/2023, 8:52 AMTgo1014
09/06/2023, 8:55 AMuli
09/06/2023, 9:03 AMTgo1014
09/06/2023, 9:04 AMstate.update { "B" }
running automatically before the test runs 😕Stylianos Gakis
09/06/2023, 9:06 AMTgo1014
09/06/2023, 9:15 AMStylianos Gakis
09/06/2023, 9:16 AMStylianos Gakis
09/06/2023, 9:17 AMTgo1014
09/06/2023, 9:18 AMStylianos Gakis
09/06/2023, 9:19 AMStylianos Gakis
09/06/2023, 9:19 AMTgo1014
09/06/2023, 9:20 AMStylianos Gakis
09/06/2023, 9:22 AMStylianos Gakis
09/06/2023, 9:24 AMTgo1014
09/06/2023, 9:27 AMStylianos Gakis
09/06/2023, 9:30 AMTgo1014
09/06/2023, 9:35 AMStylianos Gakis
09/06/2023, 9:42 AMTgo1014
09/06/2023, 11:23 AMrunTest
automatically runs everything queued. With this piece of info I changed my test to this and it worked:
@Test
fun test() {
assert(viewModel.state.value == "A")
runTest {
advanceUntilIdle()
assert(viewModel.state.value == "B")
}
}
Tgo1014
09/06/2023, 11:28 AMStylianos Gakis
09/06/2023, 11:35 AM