AndroidX测试-ActivityScenario onView()可以';找不到嵌套的子片段

AndroidX测试-ActivityScenario onView()可以';找不到嵌套的子片段,android,android-fragments,android-espresso,robolectric,androidx-test,Android,Android Fragments,Android Espresso,Robolectric,Androidx Test,我有一个activityMyActivity,它依次保存一个片段MyFragment。在MyFragment中,我使用childFragmentManager显示一个BottomSheetDialogFragment,如下所示: bottomSheetFragment.show(childFragmentManager,“bottomSheetFragment”) 下面是我在JVM中运行的测试用例(使用AndroidX测试/Robolectric) @Test fun isBottomS

我有一个activity
MyActivity
,它依次保存一个片段
MyFragment
。在
MyFragment
中,我使用
childFragmentManager
显示一个BottomSheetDialogFragment,如下所示:

bottomSheetFragment.show(childFragmentManager,“bottomSheetFragment”)

下面是我在JVM中运行的测试用例(使用AndroidX测试/Robolectric)

  @Test
  fun isBottomSheetShownTest() {
    val scenario = ActivityScenario.launch(MyActivity::class.java)

    FakeViewModle.showBottomSheet() // Posts on a LiveData to show the bottom sheet

    scenario.onActivity {
      val fragment = it.supportFragmentManager.fragments[0]
      val bottomSheet = fragment.childFragmentManager.findFragmentByTag("BottomSheetFragment")!!
      val view = bottomSheet.view!!
      val findViewById = view.findViewById<View>(R.id.bottomSheetTitle)
      val visibility = findViewById.visibility // View is present and Visible
    }

    Espresso.onView(ViewMatchers.withId(R.id.bottomSheetTitle)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
    // Throws androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching
  }
@测试
有趣的是showntest(){
val scenario=ActivityScenario.launch(MyActivity::class.java)
FakeViewModle.showBottomSheet()//在LiveData上发布以显示底部工作表
情景模拟活动{
val fragment=it.supportFragmentManager.fragments[0]
val bottomSheet=fragment.childFragmentManager.findFragmentByTag(“BottomSheetFragment”)!!
val视图=底部板材。视图!!
val findviewbyd=view.findviewbyd(R.id.bottomSheetTitle)
val visibility=findviewbyd.visibility//视图存在且可见
}
Espresso.onView(ViewMatchers.withId(R.id.bottomSheetTitle))。检查(匹配(withEffectiveVisibility(ViewMatchers.Visibility.Visibility)))
//抛出androidx.test.espresso.NoMatchingViewException:在层次结构中找不到匹配的视图
}
上述测试用例因“androidx.test.espresso.nomatchingvieweexception:在层次结构中找不到匹配的视图”而失败。 但是在
scenario.onActivity{}
中,我能够看到膨胀的底部表单片段。在调试时,我发现底部板材已完全膨胀,处于恢复状态

我的问题是:

  • Espresso.onView()包含什么内容?当我检查视图层次结构时,我看到了MyActivity和MyFragment,但没有看到底部工作表

  • 使用
    Espresso.onView()
    是否无法获取嵌套的子片段

  • 如何通过使用
    Espresso.onView()
    而不是查询
    场景.onActivity
    来断言底部表单中是否存在视图