我正在尝试使用
.testTag
测试可组合项,该可组合项位于给定可组合项调用结构的树的叶级别。代码的风格是:
@Composable
fun TopLevelComposable() {
AComposable()
}
@Composable
fun AComposable() {
LeafComposable()
}
@Composable
fun LeafComposable() {
Text(text = "stringOfText", modifier = Modifier.testTag("Text of AComposable"))
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
TopLevelComposable()
}
}
}
}
但是,我收到错误:
java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'Text of AComposable')
However, the unmerged tree contains '1' node that matches. Are you missing `useUnmergedNode = true` in your finder?
如何使用可组合项让 androidTest 接受嵌套
.testTag
?
我没有看到你在示例中调用
.assertExists()
,但错误消息有一个有用的建议。在您的仪器测试中,添加 useUnmergedTree = true
,如下所示:
composeTestRule.onNodeWithTag("Text of AComposable", useUnmergedTree = true)
.assertExists()
.assertIsDisplayed()