DeveloperKurt
DeveloperKurt

Reputation: 788

Android - Testing Fragments With Espresso by Using launchFragmentInContainer Never Completes

My test is never running to completion and I have absolutely no idea why. I can see the toast displayed on my phone's screen. There is absolutely nothing in the logs.

@RunWith(AndroidJUnit4::class)
@SmallTest
class BaseDataFragmentUITest
{
   @Test
   fun isDisplayingToastWhenFAILED_TO_UPDATE()
   {

    val fragmentScenario = launchFragmentInContainer<TestBaseDataFragmentImp>()
    val toastString: String = context.resources.getString(com.developerkurt.gamedatabase.R.string.data_update_fail)

  
    fragmentScenario.onFragment {
        it.handleDataStateChange(BaseRepository.DataState.FAILED_TO_UPDATE)
  
        onView(withText(toastString)).inRoot(withDecorView(not(it.requireActivity().getWindow().getDecorView()))).check(matches(isDisplayed()))

       }
    
   }
}

Upvotes: 0

Views: 708

Answers (1)

DeveloperKurt
DeveloperKurt

Reputation: 788

Apparently, Espresso assertions shouldn't be made inside of the onFragment block. So when I wrote the test like this it worked:

    @Test
    fun isDisplayingToastWhenFAILED_TO_UPDATE()
    {

        val fragmentScenario = launchFragmentInContainer<TestBaseDataFragmentImp>()
        val toastString: String = context.resources.getString(com.developerkurt.gamedatabase.R.string.data_update_fail)

        var decorView: View? = null

        fragmentScenario.onFragment {
            it.handleDataStateChange(BaseRepository.DataState.FAILED_TO_UPDATE)
            decorView = it.requireActivity().getWindow().getDecorView()
        }
        
onView(withText(toastString)).inRoot(withDecorView(not(decorView!!))).check(matches(isDisplayed()))

    }

Upvotes: 1

Related Questions