在我的应用程序中,我正在使用导航控制器使用Kotlin lang、Espresso框架和Mockito测试导航组件。应用程序有多个片段。很少有测试运行良好。但无法处理以下场景
只是想了解它的工作原理。为另一个测试提供下面的代码,该测试在fragment4到fragment2之间运行良好(其中fragment4总是重定向到fragment2,即单一导航)
@Test
fun testNavigationFromGameOverToGameFragment() {
// Create a mock NavController
val mockNavController = mock(NavController::class.java)
// Create a graphical FragmentScenario for the TitleScreen
val gameOver = launchFragmentInContainer<GameOverFragment>(themeResId = R.style.AppTheme)
// Set the NavController property on the fragment
gameOver.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
}
// Verify that performing a click prompts the correct Navigation action
onView(ViewMatchers.withId(R.id.tryAgainButton)).perform(ViewActions.click())
verify(mockNavController).navigate(R.id.action_gameOverFragment_to_gameFragment)
}