Testing Koin viewmodel: Cannot invoke setValue on a background thread

28 views Asked by At

Trying to create JUnit test for Viewmodel using Koin DI. Running the first test I've got this issue: Cannot invoke setValue on a background thread. So how should I test Viewmodel correctly?

Test:

class TripTrackerPerspectiveViewModelTest: KoinTest {

    private val viewModel: MyTrackerViewModel by inject()

    @Test
    fun initTest(){
        Timber.d("initTest = ${viewModel.getTodayDate()}")
    }

}

Viewmodel:

class TripTrackerPerspectiveViewModel(
    private val locationManager: LocationManagerInterface,
    private val settingsManager: SettingsManagerInterface): BaseViewModel() {

    val currentStationIndex = MutableLiveDataUpdated<Int>().also { it.value = 0 } //error is here
}
1

There are 1 answers

0
Boken On

Instead of:

MutableLiveDataUpdated<Int>().also { it.value = 0 }

you can use:

MutableLiveData<Int>().also { it.postValue(0) }

or just:

MutableLiveData(0)