Issue
We can get the string resource in Composable through stringResource like
@Composable
fun Heading(
@StringRes textResource: Int
) {
Text(
text = stringResource(id = textResource),
color = colorBlack,
)
}
But how can we get this string resource in composable test.
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()
}
}
Solution
Create compose rule via createAndroidComposeRule
and then you will be able to access activity.getString()
method
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
val activity = composeTestRule.activity
@Test
fun testDisplayAndClickable() {
val home = composeTestRule.activity.getString(R.string.home)
}
Answered By - jorieitomuke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.