我们可以通过 stringResource 来获取 Composable 中的字符串资源,例如
@Composable
fun Heading(
@StringRes textResource: Int
) {
Text(
text = stringResource(id = textResource),
color = colorBlack,
)
}
Run Code Online (Sandbox Code Playgroud)
但是我们如何在可组合测试中获取这个字符串资源。
class HeadingTest {
@get:Rule
val composeTestRule = createComposeRule()
@ExperimentalComposeUiApi
@Test
fun headingTest() {
// Start the app
composeTestRule.setContent {
AppTheme {
// In Compose world
Heading(textResource = R.string.some_text)
}
}
//How can I access string resource here
composeTestRule.onNodeWithText(???).assertExists()
}
}
Run Code Online (Sandbox Code Playgroud) android android-testing android-jetpack android-jetpack-compose