How do you launch a fragment in the test scope that contains a LiveData observer with Robolectric.
My fragment is observing the liveData from the ViewModel. If I remove this section of code the test runs.
fun observerData() {
viewModel.MyLiveData.observe(viewLifecycleOwner, Observer {
...
})
}
The test uses a fragment scenario to start the fragment.
@RunWith(RobolectricTestRunner::class)
class MyFragmentTest {
@Test
fun myTest() {
val ViewModel: MyViewModel = mock(MyViewModel::class.java)
val scenario = launchFragmentInContainer(
factory = MainFragmentFactory(ViewModel),
fragmentArgs = null,
themeResId = R.style.Theme_MyTheme
)
}
}
the test returns the following errors.
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.
I've tried setting the main looper to idol but still get the same error.