Hi! :wave: Testing + AnimatedNavController I want ...
# compose
d
Hi! 👋 Testing + AnimatedNavController I want to test a composable that has its own navigation. I pass in an instance of
TestNavHostController
as
NavHostController
parameter (defaults to
rememberAnimatedNavController
) but get an exception when the building the navgraph:
Copy code
androidx.navigation.testing.TestNavigatorProvider$navigator$1 cannot be cast to com.google.accompanist.navigation.animation.AnimatedComposeNavigator
  at com.google.accompanist.navigation.animation.NavGraphBuilderKt.composable(NavGraphBuilder.kt:124)
Doesnt the accompanist nav animation lib support testing yet? (code in 🧵)
✅ 1
Composable to be tested:
Copy code
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun FeedbackRoot(
    navController: NavHostController = rememberAnimatedNavController(),
    viewModel: FeedbackViewModel = viewModel()
) {
...
AnimatedNavHost(
        navController = navController,
        startDestination = "feedbackTypePicker",
    ) {
        composable(
            route = "feedbackTypePicker",
The exception is thrown from the first call to
composable
in the lambda of the AnimatedNavHost. The test:
Copy code
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun feedbackScreen_clickingTypeNavigatesToCategoryScreen() {
    val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
    composeTestRule.setContent {
        NTTheme {
            FeedbackRoot(
                navController = navController,
                viewModel = feedbackViewModel,
                onFinish = {}
            )
        }
    }
   ...
}
Turns out using
Copy code
@get:Rule
val composeTestRule = createComposeRule()
instead of
Copy code
val composeTestRule = createAndroidComposeRule<FeedbackActivity>()
fixed my issues. I thought I had to inject a
TestNavHostController
instance instead of using the default
AnimatedNavHostController
, but apparently not, the test runs just fine with it 🙂
🙌 1