title: How test a ViewModel function that launch a viewModelScope coroutine? Android Kotlin
body: <p>I´m trying to figure out the simplest way to test this kind on function members, I´ve seen more complex cases like <a href="https://stackoverflow.com/questions/55765190/coroutines-unit-testing-viewmodelscope-launch-methods">Coroutines - unit testing viewModelScope.launch methods</a> but didn´t solved</p>
  <blockquote>
  <p>ListScreenViewModel.kt</p>
  </blockquote>
  <pre><code>@HiltViewModel
  class ListScreenViewModel @Inject constructor(): ViewModel() {
  
      private var _itemsNumber = mutableStateOf(0)
  
      private var _testList = mutableStateOf(listOf&lt;String&gt;())
      val testList = _testList
  
      fun addItem() {
          viewModelScope.launch {
              _itemsNumber.value++
              _testList.value += (
                  &quot;Item ${_itemsNumber.value}&quot;
                  )
          }
      }
  }
  </code></pre>
  <blockquote>
  <p>ListScreenViewModelTest.kt</p>
  </blockquote>
  <pre><code>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)
      }
  }
  </code></pre>
  <blockquote>
  <p>Error message</p>
  </blockquote>
  <pre><code>Exception in thread &quot;Test worker&quot; java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
  
  </code></pre>