Android espresso does not navigate to other Fragment?

Viewed 281

In my project that makes open new fragment with NavigationComponenet when click button. I want to test if fragment open when click button, But it don't work properly. Only it click button and does not open another fragment. So, I can't test if it works. Why it does not navigate?

@RunWith(AndroidJUnit4::class)
class WelcomeFragmentTestDoctor {
    val phoneHelper = PhoneHelper
    private lateinit var scenario: FragmentScenario<WelcomeFragment>

    @Before
    fun setup() {
        scenario = launchFragmentInContainer(themeResId = R.style.AppTheme)
        scenario.moveToState(Lifecycle.State.STARTED)
        Intents.init()
    }

    @After
    fun tearDown(){
        Intents.release()
    }



@Test
fun clickApplyAsADoctor(){
    val navController = TestNavHostController(
        ApplicationProvider.getApplicationContext())

    scenario.onFragment { fragment ->
        navController.setGraph(R.navigation.auth_navigation)
        Navigation.setViewNavController(fragment.requireView(), navController)
    }
    onView(withId(R.id.buttonDoctor)).perform(click())
    Assert.assertEquals(navController.currentDestination?.id, R.id.action_welcomeFragment_to_doctorRegistrationFragment)
    }
}

fragment_doctor_registration.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/_14sdp"
            android:paddingBottom="@dimen/_14sdp"
            app:layout_constraintTop_toTopOf="parent">

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

DoctorRegistrationFragment.kt

class DoctorRegistrationFragment : Fragment() {
    private lateinit var mBinding: FragmentDoctorRegistrationBinding
    private val mViewModel: DoctorRegistrationViewModel by inject()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = FragmentDoctorRegistrationBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        DoctorRegistrationComponent.inject()

        with(mBinding) {
            backButton.setOnCrashOnClickListener {
                findNavController().popBackStack()
            }
            btnSend.setOnCrashOnClickListener {
                mViewModel.onEvent(
                    DoctorRegistrationInteractions.RegisterStart(fdr_name.text, fdr_surname.text,
                        fdr_title.text, fdr_diploma.text, fdr_branch.text,
                        inputLogin.lifEdittext.text.toString(), fdr_email.text, fdr_address.text,
                        fdr_company.text, fdr_tax.text

                    ))
            }
        }

        with(mViewModel) {
            actions.map { it.getContentIfNotHandled() }.onEach(::handleActions).launchIn(viewLifecycleOwner.lifecycleScope)
        }
    }

    private fun handleActions(action: DoctorRegistrationActions) {
        when (action) {
            is DoctorRegistrationActions.ErrorMessage   -> PopupMessage.error(requireActivity(),message = action.message)
            DoctorRegistrationActions.Init              -> { }
            is DoctorRegistrationActions.SuccessMessage -> {
                PopupMessage.success(requireActivity(), message = action.message)
                findNavController().popBackStack()
            }
        }
    }
}
1 Answers

You need to check the fragment id not the Action id

Assert.assertEquals(navController.currentDestination?.id, R.id.doctorRegistrationFragment)

Assuming that doctorRegistrationFragment is the id of your fragment tag in your nav graph

    <fragment
        android:id="@+id/doctorRegistrationFragment"
        /* rest of attrs */ >

Related