dimanche 29 novembre 2020

How to test android navigation safeargs

Lets say I have 2 fragments: FirstFragment and SecondFragment. I will navigate from FirstFragment to SecondFragment passing through a message argument using safeargs. Where message is just a string.

In my FirstFragment I have a method:

fun navigateToSecond(msg: String){
    var action = FirstFragmentDirections.actionToSecond(msg)
    navController.navigate(action)
}

I can use this method like this:

navigateToSecond("my message")

Then in the SecondFragment I will get the message:

override fun onStart() {
    super.onStart()
    arguments?.let {
        var args = SecondFragmentArgs.fromBundle(it)
        val message = args.message
        // use message here
    }
}

I would like to know is there a way to test this, in particular I would like to test that the appropriate message is sent to SecondFragment when I call navigateToSecond and that SecondFragment correctly receives the message.

I would like to test this using mockito or robolectric.

I tried using mocking to check whether the appropriate methods are called on navController but it was hard and didn't worked out.

So, is there a good or well known way to test the safeargs in android?

Thanks!

Aucun commentaire:

Enregistrer un commentaire