0

I'm rendering UI composables conditionally, based on UiState, however, tagging composables at the root with testTag doesn't make them findable in instrumentation tests.

Error:

 java.lang.AssertionError: Failed: assertExists.
 Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'string')

The composable is passed as content parameter (content: @Composable () -> Unit) to another composable. The code is of the form:

 ParentComposable(
     <parameters>,
     content = {
         when (uiState) {
             is UiState.Loading -> {
                 LoadingScreen(
                     modifier =
  Modifier.testTag("string")
                 )
             }
         }
     }
 )

I'm mockking the UiState (it's stored in the ViewModel) so I'm sure I should be hitting the condition from this point of view. I even see the screen on my device and yet the test fails to find the node. Really at odds here how to fix this.


What I tried

LoadingScreen composable fails to be found. I tried the execution of assertion with useUnmergedTree set to true but that fails too.

2
  • Are you sure the that when the test is running, the uiState is actually equal to UiState.Loading (you actually see this screen during the test)? In your LoadingScreen composable, are you applying the passed in modifier to another composable and not accidentally doing modifier = Modifier (notice the CAPITAL M)?
    – zecuse
    Commented Jun 27 at 3:31
  • I'm sure that when the test is running, the uiState is actually equal to UiState.Loading - I actually see this screen during the test. And, yes, I'm applying the passed in modifier. Commented Jun 27 at 5:26

1 Answer 1

0

I debugged further and realized the modifier that was passed down, ultimately was used by a composable I was conditionally not showing. Once I changed the tagging to the correct composables the tests succeeded.

Not the answer you're looking for? Browse other questions tagged or ask your own question.