Reputation: 1741
I want to test DialogFragment using androidx.fragment:fragment-testing
lib.
I call launchFragmentInContainer
and moveToState(Lifecycle.State.RESUMED)
, but onCreateDialog
is not called in this fragment.
@Test
fun `submit search - presenter state is changed`() {
val p: PinCatsPresenter = F.presenter(PinCatsPresenter.COMPONENT_ID)!!
launchFragmentInContainer<PinCatsDialog>().let { scenario ->
scenario
.moveToState(Lifecycle.State.RESUMED)
.onFragment { fragment ->
assertFalse(p.state.isFiltered)
fragment.dialog!!.findViewById<SearchView>(R.id.search_field).let {
it.isIconified = false
it.setQuery("ea", true)
}
awaitUi()
assertTrue(p.state.isFiltered)
assertEquals(3, p.state.count)
}
}
}
I debug the app, and ensured that onCreateDialog
is called earlier than onResume
, but in this test scenario onCreateDialog
is not called, so fragment.dialog
is null.
What should I call onFragmentScenario
so my dialog would be created?
Upvotes: 4
Views: 784
Reputation: 1741
This is described in the official documentation. We need to call launchFragment
instead of launchFragmentInContainer
:
launchFragment<PinCatsDialog>().let { scenario ->
scenario
.moveToState(Lifecycle.State.RESUMED)
.onFragment { fragment ->
// Code here
}
}
Upvotes: 3