Moref
11/04/2025, 3:16 PMinit in class when init changed the state in testing:
VM:
package com.example.test
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.container
data class CounterViewModelState(val count: Int)
class CounterViewModel : ContainerHost<CounterViewModelState, Nothing>, ViewModel() {
override val container: Container<CounterViewModelState, Nothing> = viewModelScope.container(
CounterViewModelState(count = 0)
)
init {
intent{
reduce {
state.copy(count = 1)
}
}
}
fun increment() {
intent {
reduce {
state.copy(count = state.count + 1)
}
}
}
fun decrement() = intent {
reduce {
state.copy(count = state.count - 1)
}
}
}
Test will not work:
class CounterViewModelTest {
@Test
fun `Test 1`() = runTest {
CounterViewModel().test(this) {
expectState { copy(count = 1) }
}
}
}
error message:
No value produced in 3s
app.cash.turbine.TurbineAssertionError: No value produced in 3s
at app//app.cash.turbine.TurbineAssertionError$Companion.invoke(TurbineAssertionError.kt:32)
at app//app.cash.turbine.ChannelKt.awaitEvent(channel.kt:89)
at app//app.cash.turbine.ChannelKt$awaitEvent$1.invokeSuspend(channel.kt)
but when do this it works:
class CounterViewModelTest {
@Test
fun `Test 1`() = runTest {
CounterViewModel().test(this) {
containerHost.increment()
expectState { copy(count = 1) }
}
}
}
is there away to make it works