0

I want to test that on click, user is being navigated to the correct screen.

I can see the correct button is clicked after I do performClick() in my tests but it is saying the call happened with different arguments. I can't figure out what is causing the error.

@Destination
@Composable
fun UserScreen(
    navigator: DestinationsNavigator,
) {
Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier.fillMaxWidth().padding(8.dp)
    ) {
        Button( onClick = { 
        navigator.navigate(DataCreateDestination(createType = "user"))
 }){
            Text(
                text = "Create User"
            )
        }
        Button( onClick = {
        navigator.navigate(DataCreateDestination(createType = "group"))

 }) {
            Text(
                text = "Create Group"
            )
        }
    }
}

the test now

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class UserScreenTest {
    @get:Rule
    val rule  = createAndroidComposeRule<ComponentActivity>()

    private val mockNavigation = mockk< DestinationsNavigator>(relaxed = true)
    private fun hasText(@StringRes strResId: Int) = hasText(rule.activity.getString(strResId))

    @Before
    fun init(){
        rule.waitForIdle()
    }


    @Test
    fun testNavigateToCreateUserScreen() {

        coEvery {
            mockNavigation.navigate(DataCreateDestination(createType="user"))
        } returns Unit

        rule.setContent {
            UserScreen(navigator =  mockNavigation)
        }

        rule.onNode(hasText("Create User")).assertIsDisplayed().assertHasClickAction().performClick()
        coVerify (exactly = 1){ mockNavigation.navigate(DataCreateDestination(createType="user"))}
    }

}

the test fails with the following message:

java.lang.AssertionError: Verification failed: call 1 of 1: DestinationsNavigator(#2).navigate(eq(DataCreateDestination$invoke$2@c570f86), eq(false), eq(lambda {}))). Only one matching call to DestinationsNavigator(#2)/navigate(Direction, Boolean, Function1) happened, but arguments are not matching:
[0]: argument: DataCreateDestination$invoke$2@aebacc4, matcher: eq(DataCreateDestination$invoke$2@c570f86), result: -
[1]: argument: false, matcher: eq(false), result: +
[2]: argument: lambda {}, matcher: eq(lambda {}), result: +
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hobo
  • 43
  • 6

0 Answers0