android - Android 测试 - 如何验证 navController.currentDestination.arguments?
问题描述
我正在尝试使用浓缩咖啡和导航组件验证传递给我的新片段的数据。我从这里举了一个例子来简化这个问题。
@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)
}
我不知道如何转换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()
推荐阅读
- java - 如何让@Query 在 JPA 中执行 ContainingIgnoreCase 函数?
- asp.net-mvc - MVC 中代码类(POCO、模型等)的正确位置是什么?
- javascript - 在 JS 中难以选择页面上的元素
- java - ArrayList 无法解析为 JSP 文件中的类型
- javascript - 在选择日期完整日历上显示模式弹出窗口时出错
- sql - PostgreSQL:获取所有包含大写字母的行
- python - 如何在python的for循环中打印输出之前设置标题?
- c - STM32L4R5 的 SystemClock 配置问题
- javascript - 在 Shadow DOM 中的 setTimeout 中的 event.target 发生了变化?
- go - 如何在无限 for 循环中实现 goroutine;for{} 在等待组完成后重试?