Anyone that have used turbine for testing SharedSt...
# coroutines
p
Anyone that have used turbine for testing SharedStateFlow have faces issues when in
viewModel
have the call in the
init{}
? Because the way to test in turbine is to create the
viewModel
and then get the
sharedStateFlow
and do the calls of the
viewModel
but what if the code is in
init
when creating the
viewModel
is already called this
init
so I can not test the first state of this
sharedFlow
right? Example :
Copy code
@Test 
fun test = runTest { 
  val viewModel = createViewModel() <-- already call the init
  viewModel.stateFlow.test { 
    //What should I call here? If it's in init...
  }
}
j
you should be able to get whatever was emitted inside
init
by calling
awaitItem()
You might want to cast your
StateFlow
to a regular
Flow
to prevent emitted values to be skipped if your init emit several values in a short time.
a
you've encountered one of many reasons to avoid launching autonomous coroutines as a side effect of object construction 🙂