I´m trying to figure out the simplest way to test this kind on function members, I´ve seen more complex cases like Coroutines - unit testing viewModelScope.launch methods but didn´t solved
ListScreenViewModel.kt
@HiltViewModel
class ListScreenViewModel @Inject constructor(): ViewModel() {
private var _itemsNumber = mutableStateOf(0)
private var _testList = mutableStateOf(listOf<String>())
val testList = _testList
fun addItem() {
viewModelScope.launch {
_itemsNumber.value
_testList.value = (
"Item ${_itemsNumber.value}"
)
}
}
}
ListScreenViewModelTest.kt
class ListScreenViewModelTest{
private lateinit var viewModel: ListScreenViewModel
@Before
fun setup(){
viewModel = ListScreenViewModel()
}
@Test
fun `add an item to the list of items`(){
val numberOfItems = viewModel.testList.value.size
viewModel.addItem()
assert(viewModel.testList.value.size == numberOfItems 1)
}
}
Error message
Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
CodePudding user response:
You need to use something called TestCoroutineDispatcher
during local unit tests & the best way to use it creating a Rule
.
You can read about this in detail here: https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-survey#3
I would recommend you go through this whole codelab. It will be really helpful.
For the solution
Add this dependency:
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2") {
// https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android
exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
}
Then Create a rule like this in your test directory:
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@ExperimentalCoroutinesApi
class MainCoroutineRule(val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) :
TestWatcher(),
TestCoroutineScope by TestCoroutineScope(dispatcher) {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
cleanupTestCoroutines()
Dispatchers.resetMain()
}
}
Use it like this:
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Rule
import org.junit.Test
class ListScreenViewModelTest {
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
private lateinit var viewModel: ListScreenViewModel
@Before
fun setup(){
viewModel = ListScreenViewModel()
}
@Test
fun `add an item to the list of items`(){
val numberOfItems = viewModel.testList.value.size
viewModel.addItem()
assert(viewModel.testList.value.size == numberOfItems 1)
}
}