I have created a LoginFragment which navigates to another one when a login is successful using the Navigation Architecture Component. Works fine but the test for it is failing. It is quite a close mirror of the GithubBrowser sample and my test is very similar to the clickRepo
test here, except I am testing that a new page is loaded when a successful Resource is posted instead of upon a click.
Error:
Wanted but not invoked:
navController.navigate(
app.ui.login.LoginFragmentDirections$ShowSelectMerchant@377f3c27
);
LoginFragmentTest.kt
@Test
fun success(){
val user = TestUtil.createUser(email)
userData.postValue(Resource.success(user))
System.out.println("Test NavController Hash: " + testFragment.navController.hashCode())
verify(testFragment.navController).navigate(LoginFragmentDirections.showSelectMerchant().matcher())
}
LoginFragment.kt
override fun openSelectMerchantFragment() {
System.out.println("Real NavController Hash: " + navController().hashCode())
navController().navigate(LoginFragmentDirections.showSelectMerchant())
}
openSelectMerchantFragment
is called when a successful Resource is posted to the live userData. Is there an obvious reason why the test is failing?
I can see that the test fragment's mocked nav controller and the one that is called in the fragment itself have the same hash value so I am pretty sure it's not a problem with how I have mocked the activity, fragment or nav controller. I can also obviously see that the navigate() function is definitely called.