我使用Jetpack在应用程序上运行了测试。这些测试是独立通过的,但在连续运行时,我会收到:
java.lang.IllegalStateException:没有被摧毁的事件
还有一个调用堆栈,包括我的入口点中的一些行,它们似乎表明NavController实现是罪魁祸首。这个问题在运行在同一设备上的非仪器化的构建中是不可重复的,因此我有一种感觉,那就是我如何在@ have函数中实现AppState创建。如有任何建议,将不胜感激。
java.lang.IllegalStateException: no event up from DESTROYED
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:263)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:121)
at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.kt:173)
at androidx.navigation.NavBackStackEntry.handleLifecycleEvent(NavBackStackEntry.kt:157)
at androidx.navigation.NavController.lifecycleObserver$lambda-2(NavController.kt:184)
at androidx.navigation.NavController.$r8$lambda$QcvT-AhOyhL9f0B2nrlZ1aMydmQ(Unknown Source:0)
at androidx.navigation.NavController$$ExternalSyntheticLambda0.onStateChanged(Unknown Source:2)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
**at androidx.navigation.NavController.setLifecycleOwner(NavController.kt:2119)**
at androidx.navigation.NavHostController.setLifecycleOwner(NavHostController.kt:54)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:105)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:69)
**at com.roadbytes.ui.main.RoadBytesAppKt$CreateRoadBytesApp$1$2.invoke(RoadBytesApp.kt:198)
at com.roadbytes.ui.main.RoadBytesAppKt$CreateRoadBytesApp$1$2.invoke(RoadBytesApp.kt:196)**
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1$bodyContentPlaceables$1.invoke(Scaffold.kt:316)
at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1$bodyContentPlaceables$1.invoke(Scaffold.kt:314)安装函数如下所示:
@Before
fun setupAppWithFakes() {
composeTestRule.setContent {
val navController = rememberNavController()
val tripsRepo = FirebaseRepo(
emulate = true,
firebaseDBPath = resources.getString(R.string.db_path_trips),
logTag = resources.getString(R.string.log_tag_trips_repo),
entityFactory = TripFactory())
val appState = RoadBytesAppState.getInstance(
with(RoadBytesAppState){
mapOf(
this.CONTEXT_KEY to LocalContext.current,
this.NAV_CONTROLLER_KEY to navController,
this.AUTH_HANDLER_KEY to authHandlerFake,
this.TRIPS_REPO_KEY to tripsRepo
)
}
)
CreateRoadBytesApp(appState)
}
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(tripsNavButtonTag).performClick()
}我的切入点可合成如下:
@ExperimentalMaterialApi
@Composable
fun CreateRoadBytesApp(
appState: RoadBytesAppState
) {
RoadBytesTheme {
Scaffold(
bottomBar = { RoadBytesBottomNavBar(appState, Screens.navigableScreens()) }
) { innerPadding ->
NavHost(
navController = appState.navController,
startDestination = Screens.RecordTripScreen.route,
modifier = Modifier.padding(innerPadding)
){
composable(Screens.RecordTripScreen.route) { RecordTripScreen(appState) }
composable(Screens.SignInScreen.route) { SignInScreen(appState) }
composable(Screens.SignUpScreen.route) { SignUpScreen(appState) }
composable(Screens.TripsScreen.route) { TripsScreen(appState) }
composable(Screens.VehiclesScreen.route) { VehiclesScreen(appState) }
}
}
}
}发布于 2022-06-07 09:45:23
我假设您使用的是createComposeRule()而不是createAndroidComposeRule(MainActivity)。重复检查包含UI测试的模块的androidTestImplementation文件的依赖项:特别是debugImplementation和build.gradle
// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createComposeRule, but not createAndroidComposeRule:
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")https://stackoverflow.com/questions/71850860
复制相似问题