Hello! According to the new coroutines version how...
# coroutines
p
Hello! According to the new coroutines version how would I test a
ViewModel
like this?
Copy code
@HiltViewModel
class FooViewModel @Inject constructor(
    @DispatchersIO private val dispatcher: CoroutineDispatcher,
    val useCase: MyUseCase,
) : ViewModel() {
 
    private val _state = MutableStateFlow<State>(State.Loading)
    val state: StateFlow<State> = _state

    init {
        getFoo()
    }

    private fun getFoo() {
        viewModelScope.launch(dispatcher) {
            usecase()
                .fold(
                    {
                        _state.value = State.Error
                    },
                    { items ->
                        _state.value = State.Data(items)
                    })
        }
    }
I've tried with turbine but don't know how to test the
init
method, is there any easy way to do it with the newer version of coroutines? I can't verify first is
State.Loading
then if success the state is
State.Data
Also I was reading this https://github.com/Kotlin/kotlinx.coroutines/issues/3143