喷气背包撰写测试在连续运行时会导致"java.lang.IllegalStateException: no event up from DESTROYED"



我使用Jetpack Compose在应用程序上运行了检测测试。测试是独立通过的,但当连续运行时,我会收到:

java.lang.IollegalStateException:DESTROYED 没有事件

以及一个调用堆栈,其中包括我的入口点组合中的一些行,这些行似乎表明NavController实现是罪魁祸首。在同一设备上运行的未插入指令的构建中,这个问题是不可重现的,所以我有一种感觉,这就是我在@Before函数中实现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) }
}
}
}
}

我假设您使用的是createComposerRule((,而不是createAndroidComposerrule(MainActivity(。仔细检查包含UI测试的模块的build.gradle文件的以下依赖项:特别是androidTestImplementationdebugImplementation

// 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")

测试Compose布局|Jetpack Compose|Android开发者

最新更新