首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jetpack撰写测试在连续运行时会导致"java.lang.IllegalStateException:没有事件从破坏中上升“

Jetpack撰写测试在连续运行时会导致"java.lang.IllegalStateException:没有事件从破坏中上升“
EN

Stack Overflow用户
提问于 2022-04-13 01:41:20
回答 1查看 1.2K关注 0票数 0

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

java.lang.IllegalStateException:没有被摧毁的事件

还有一个调用堆栈,包括我的入口点中的一些行,它们似乎表明NavController实现是罪魁祸首。这个问题在运行在同一设备上的非仪器化的构建中是不可重复的,因此我有一种感觉,那就是我如何在@ have函数中实现AppState创建。如有任何建议,将不胜感激。

代码语言:javascript
复制
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)

安装函数如下所示:

代码语言:javascript
复制
@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()
}

我的切入点可合成如下:

代码语言:javascript
复制
@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) }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-07 09:45:23

我假设您使用的是createComposeRule()而不是createAndroidComposeRule(MainActivity)。重复检查包含UI测试的模块的androidTestImplementation文件的依赖项:特别是debugImplementation和build.gradle

代码语言:javascript
复制
// 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")

测试您的构图布局

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71850860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档