title: Unit Testing LiveData Value with Coroutines
body: <p>I want to test if the viewmodel's variable has the return value of the repository call. It is my first time using Coroutines. </p>
  
  <p>The problem here is that my event variable is returning null here:</p>
  
  <blockquote>
    <p>Assert.assertEquals(viewModel.event.getOrAwaitValue(), EventGame(games = games))</p>
  </blockquote>
  
  <p>My viewModel:</p>
  
  <pre><code>class GamesViewModel(private val repository: GameRepository) : ViewModel(), CoroutineScope {
      override val coroutineContext: CoroutineContext =
          Dispatchers.Main + SupervisorJob()
  
      private val _event = MutableLiveData&lt;EventGame&gt;()
      val event: LiveData&lt;EventGame&gt; get() = _event
  
  
      fun listGames() = this.launch {
          val ej = withContext(Dispatchers.Default) {
              repository.listGames()
          }
          _event.postValue(ej)
      }
  }
  </code></pre>
  
  <p>My unit test class</p>
  
  <pre><code>class GamesUnitTest : KoinTest{
      lateinit var viewModel: GameViewModel
  
      private val g1 = Game(id = 1, data = "01/01/2020 18:35" ...)
      private val g2 = Game(id = 2, data = "01/10/2020 18:35"...)
      private val games = listOf(g1, g2)
  
      @Mock
      lateinit var repository: GameRepository
  
      @get:Rule
      var coroutinesTestRule = CoroutinesTestRule()
  
      @get:Rule
      val instantExecutorRule = InstantTaskExecutorRule()
  
      @Mock
      lateinit var observer: Observer&lt;EventGame&gt;
  
      @After
      fun after() {
          stopKoin()
      }
  
      @Before
      fun before() {
          MockitoAnnotations.initMocks(this)
          viewModel = GameViewModel(repository)
      }
  
      @Test
      fun testListOfGames() = runBlockingTest{
          Mockito.`when`(repository.listGames()).thenReturn(EventGame(games = games))
          viewModel.event.observeForever(observer)
          viewModel.listGames()
          Assert.assertEquals(viewModel.event.getOrAwaitValue(), EventGame(games = games))
  
      }
  }
  </code></pre>
  
  <p>--Edit2</p>
  
  <p>I removed the GlobalScope.launch{}. The event is still null</p>
  
  <p>I'm not sure how to fix that.</p>