Supplying nav arguments during espresso tests

Viewed 585

I saw Stack Overflow seems to be deleting questions around this topic. No idea why..

There is nothing in the documentation around it. I am trying to test a fragment that is part of the navigation flow and therefore has its arguments supplied by "by navArgs()" injection.

How can I supply them when launching that fragment in launchFragmentInContainer?

1 Answers

Ok figured it out. Imagine you have your arguments injected in the fragment:

private val args: SumArguments by navArgs()

And the fragment itself declared in the navigation graph:

 <fragment android:id="@+id/SumFragment"
        android:name="xxx"
        android:label="xxx">
        <argument
            android:name="origin"
            app:argType="SumClass"
            app:nullable="false" />

Then you can launch your fragmentScenario like so:

val origin = SumClass()

 launchFragment<SumFragment>(
            bundleOf(
                "origin" to origin,
            )
        )

No idea how Android knows to map Arguments from Bundle but it does. Enjoy.

Related