Android espresso 无法导航到其他 Fragment?
Posted
技术标签:
【中文标题】Android espresso 无法导航到其他 Fragment?【英文标题】:Android espresso does not navigate to other Fragment? 【发布时间】:2021-12-27 11:10:07 【问题描述】:在我的项目中,单击button
时使用NavigationComponenet
打开新片段。我想测试单击按钮时是否打开片段,但它不能正常工作。只有它单击按钮并且不会打开另一个片段。所以,我无法测试它是否有效。为什么它不导航?
@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_
android:layout_>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolbar"
android:layout_
android:layout_
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()
【问题讨论】:
也请分享你的第一个片段代码! @miladsalimi 我做到了。 请添加 fragment.kt 而不是 XML。 @miladsalimi 好的,我刚刚添加了。 【参考方案1】:您需要检查fragment
id 而不是Action
id
Assert.assertEquals(navController.currentDestination?.id, R.id.doctorRegistrationFragment)
假设doctorRegistrationFragment
是导航图中片段标签的id
<fragment
android:id="@+id/doctorRegistrationFragment"
/* rest of attrs */ >
【讨论】:
我试过了。但它不起作用。我从模拟器中得知它没有打开 DoctorRegistrationFragment。所以,它找不到这个idea。以上是关于Android espresso 无法导航到其他 Fragment?的主要内容,如果未能解决你的问题,请参考以下文章
Android/Gradle espresso 测试未开始活动
Android Spinner在Espresso测试中点击后立即被解雇
Android Espresso 打算在 android 版本 > 5 上不工作