LaunchedEffect 中断浓缩咖啡测试延迟

问题描述 投票:0回答:1

我有一个测试如下:

@get:Rule(order = 1)
val composeRule: ComposeRule = createAndroidComposeRule()

@Test
fun test() {
    composeRule.setContent {
        var text by remember { mutableStateOf("test start") }
        LaunchedEffect(key1 = Unit, block = {
            text = "step 1"
            delay(200)
            text = "step 2"
        })
        Text(text = text, modifier = Modifier.testTag("test"))
    }
    composeRule.onNodeWithTag("test").assertTextEquals("step 1")
}

步骤 1 几乎立即显示,如预期,但测试在

delay
停止,直到崩溃。

androidx.compose.ui.test.junit4.android.ComposeNotIdleException: Idling resource timed out: possibly due to compose being busy.
IdlingResourceRegistry has the following idling resources registered:
- [busy] androidx.compose.ui.test.junit4.ComposeIdlingResource@a974589
All registered idling resources: Compose-Espresso link
at androidx.compose.ui.test.junit4.EspressoLink_androidKt.rethrowWithMoreInfo(EspressoLink.android.kt:136)
at androidx.compose.ui.test.junit4.EspressoLink_androidKt.runEspressoOnIdle(EspressoLink.android.kt:110)
at androidx.compose.ui.test.junit4.EspressoLink.runUntilIdle(EspressoLink.android.kt:79)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.waitForIdle(ComposeUiTest.android.kt:336)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment.access$waitForIdle(ComposeUiTest.android.kt:228)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidComposeUiTestImpl.waitForIdle(ComposeUiTest.android.kt:406)
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidComposeUiTestImpl.setContent(ComposeUiTest.android.kt:485)
at androidx.compose.ui.test.junit4.AndroidComposeTestRule.setContent(AndroidComposeTestRule.android.kt:340)
at pl.shvagier.district.ContactTest.test(ContactTest.kt:51)
... 54 trimmed

我的代码中是否缺少某些内容?我是不是做错了什么?

编辑

我发现

StandardTestDispatcher
在基础测试中被用作主要内容。如果没有时间提前,测试停止也就不足为奇了。现在也不例外,但仍然
LaunchedEffect
停在第一个暂停点。

android android-jetpack-compose android-espresso
1个回答
0
投票

奇怪的是,文档中没有提及它(或者我只是没有遇到它)。 LaunchedEffect适用于已经模拟了时间的协程,所以这个时间需要提前。

 composeRule.mainClock.advanceTimeBy(200)
 // further assertions

一切都像魅力一样。

© www.soinside.com 2019 - 2024. All rights reserved.