Som*_*der 3 navigation testing android casting android-espresso
我正在尝试使用浓缩咖啡和导航组件验证传递给我的新片段的数据。我从这里拿了一个例子来简化这个问题。
@Test
fun testNavigationToInGameScreen() {
// Create a TestNavHostController
val navController = TestNavHostController(
ApplicationProvider.getApplicationContext())
navController.setGraph(R.navigation.trivia)
// Create a graphical FragmentScenario for the TitleScreen
val titleScenario = launchFragmentInContainer<TitleScreen>()
// Set the NavController property on the fragment
titleScenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), navController)
}
// Verify that performing a click changes the NavController’s state
onView(ViewMatchers.withId(R.id.play_btn)).perform(ViewActions.click())
assertThat(navController.currentDestination?.id).isEqualTo(R.id.in_game)
// HERE IS WHERE I'M TRYING TO VALIDATE ARGUMENTS
val currentDestinationArgs = navController.currentDestination.arguments
val expectedArguments = bundleOf(ARG_A to true)
assertEquals(currentDestinationArgs, expectedArguments)
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何转换currentDestinationArgs
到一个包来验证
currentDestinationArgs
. 该currentDestinationArgs
领域是MutableMap<String, NavArgument>
我所知道的。有没有人想出如何测试这种东西?提前致谢。
currentDestination
不是正确的 API -NavDestination
从代表当前目的地的图表中返回。
你真正想看的是backStack:
// Get the arguments from the last destination on the back stack
val currentDestinationArgs = navController.backStack.last().arguments
// Use the Truth extension on Bundle from androidx.test.ext:truth:1.3.0-rc01
assertThat(currentDestinationArgs).bool(ARG_A).isTrue()
Run Code Online (Sandbox Code Playgroud)