Closed hongbeomi closed 2 years ago
You can replace requireParentFragment()
with parentFragment() ?: this
as a workaround for testing although it will rob you of the IllegalStateException
being thrown when fragment is being used without a parent.
The lack of a parent fragment in the test is the likely issue. I think the better workaround though would be to make your test more closely approximate your production code by actually wrapping your child fragment in a parent Hilt fragment in your test. So instead of using launchFragmentInHiltContainer
(which just uses a Hilt activity) to launch your fragment directly, I would create a test parent Hilt fragment that just adds the fragment under test to itself, and then use launchFragmentInHiltContainer
on your test parent fragment.
Thank you! @dmapr @Chang-Eric
In the case of the above code, the following error occurs when attempting to generate the
AFragment
as alaunchFragmentInHiltContainer
when writing a test code.