我遇到的情况是,在启动画面片段中,应用程序执行了一些请求。
完成所有这些操作后,片段的视图模型将在实时数据(initAppResult
)上发出“加载结果”。根据此结果,应用程序的行为方式会有所不同。
更具体地说,当启动屏幕的viewModel表示后端处于维护模式时,应用程序将显示一个包含相应消息的对话框。
这是测试:
var fragment: SplashScreenFragment? = null
navController = TestNavHostController(ApplicationProvider.getApplicationContext())
navController.setViewModelStore(ViewModelStore())
navController.setGraph(R.navigation.nav_graph)
navController.setCurrentDestination(R.id.splashScreenFragment)
scenario = launchFragmentInContainer(Bundle(), R.style.Theme_AppCompat) {
fragment = SplashScreenFragment()
fragment!!.also {
it.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
Navigation.setViewNavController(it.requireView(), navController)
}
}
}
}
fragment!!.viewModel.initAppResult.postValue(InitAppModel.SERVICE_MAINTENANCE)
assertDisplayed(R.string.server_maintenance_dialog_title) // a Barista function
它失败,但出现以下异常:
com.schibsted.spain.barista.internal.failurehandler.BaristaException: No view matching (with string from resource id: <2131755297>) was found
但是,如果我使用以下断言而不是assertDisplayed
来测试它,它将成功:
assertThat(navController.currentDestination?.id, equalTo(R.id.alertDialogFragment))
因此导航已经完成,但似乎第二个片段并未真正创建(调试器不会传递第二个片段的构造函数,也不会通过其onCreate / onCreateView方法)。
navController的配置中是否缺少某些内容,或者我做错了什么?