lundi 20 avril 2020

How to test intent form fragment in Robolectric 4 with fragment screnario?

How to test intent from a fragment in Robolectric 4 with fragment scenario when FragmentA in ActivityA should open ActivityB. The intent is always null.

@Test
fun testListFragment() {
    val args = Bundle()
    val scenario = launchFragmentInContainer<MovieListFragment>(args)

    scenario.onFragment { fragment ->
        val rvMovies = fragment.view?.findViewById<RecyclerView>(R.id.rvMovies)
        rvMovies?.getChildAt(0)?.performClick()

        val expectedIntent = Intent(fragment.requireActivity(), MovieDetailActivity::class.java)
        val actualIntent = (shadowOf(fragment.requireActivity()) as ShadowActivity).nextStartedActivity // actualIntent always return null

        assertNotNull(expectedIntent)
        assertNotNull(actualIntent)

        assertTrue(expectedIntent.filterEquals(actualIntent))
    }

    scenario.recreate()
}

Aucun commentaire:

Enregistrer un commentaire