title: Robolectric start a fragment that has an observer
body: <p>How to start a fragment with a LiveData observer in the test scope with Robolectric</p>
  <p>Fragment</p>
  <pre><code>class MyFragment(private val viewModel: MyViewModel) : Fragment() {
  
      ...
  
      fun myObserver {
          ... 
          // If I remove this observer the test will pass. 
          viewModel.MyLiveData.observe(viewLifecycleOwner, Observer{
              ...
          }
      }
  }
  </code></pre>
  <p>My Test uses the RobolectricTestRunner so I can launch the fragment in the test scope.</p>
  <pre><code>@RunWith(robolectricTestRunner::class) 
  class MyFragmentTest {
  
      // Executes tasks in the Architecture Components in the same thread
      @get:Rule
      var instantTaskExecutorRule = InstantTaskExecutorRule()
  
      @Test
      fun testOne() {
          val viewModel: MyViewModel = mock(MyViewModel::class.java)
          val scenario = launchFragmentInContainer(
              factory = MainFragmentFactory(viewModel),
              fragmentArgs = null
              themeResId = R.style.Theme_MyTheme
          )
          // Tried implementing shadowOf as the error suggests. 
  
      } 
  }
  </code></pre>
  <p>I get the following error when trying to run the test. I've tried setting Main looper to idle before and after instantiating the FragmentScenario.</p>
  <pre><code>java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
  
  </code></pre>
  <p>I've tried the following</p>
  <ul>
  <li>implementing a shadow class for the Main Looper.  Annotating the class with Looper mode.</li>
  </ul>
  <pre><code>@RunWith(RobolectricTestRunner::class)
  @LooperMode(LooperMode.Mode.PAUSED)
  class MyFragmentTest {
  </code></pre>
  <ul>
  <li>Adding scenario states</li>
  </ul>
  <pre><code>    scenario.moveToState(Lifecycle.State.CREATED)
      scenario.moveToState(Lifecycle.State.RESUMED)
  </code></pre>
  <p>My Test dependencies.</p>
  <pre><code>    // Test
      testImplementation 'androidx.arch.core:core-testing:2.1.0'
      testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3'
      testImplementation &quot;androidx.test.ext:junit-ktx:1.1.2&quot;
      testImplementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3&quot;
      testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
      testImplementation 'junit:junit:4.13.2'
      testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
      testImplementation &quot;org.robolectric:robolectric:4.5.1&quot;
      testImplementation &quot;org.mockito:mockito-android:2.28.2&quot;
  
      // Testing Fragments
      debugImplementation &quot;androidx.fragment:fragment-testing:1.3.2&quot;
  </code></pre>
  <p>Links I've used to find a solution'
  <a href="https://stackoverflow.com/questions/51810330/testing-livedata-transformations">Testing LiveData Transformations?</a>
  <a href="https://jeroenmols.com/blog/2019/01/17/livedatajunit5/" rel="nofollow noreferrer">https://jeroenmols.com/blog/2019/01/17/livedatajunit5/</a></p>