java.lang.IllegalArgumentException: Cannot navigate to NavDeepLinkRequest{ uri=android-app://androidx.navigation/movies/ }. Navigation graph has not been set for NavController androidx.navigation.testing.TestNavHostController@1d694be. I don't know how to set the navigation graph for navHost explicitly How to provide tests like this?
ScreenTest:
@Composable
fun TestNavGraph(navController: NavHostController) {
NavHost(navController = navController, startDestination = "start") {
composable("start") { Text("Start Screen") }
composable("movies/") { Text("Movies Screen") }
composable("movies/{movieId}") { backStackEntry ->
val movieId = backStackEntry.arguments?.getString("movieId")
Text("Movie Details Screen for $movieId")
}
}
}
class ScreensTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Test
fun testMoviesNavigation() {
// Create a test NavController
val navController = TestNavHostController(composeTestRule.activity)
val screens = Screens(navController)
// Setup your Composable with the TestNavHostController
composeTestRule.activity.setContent {
TestNavGraph(navController = navController)
}
// Perform navigation
screens.movies()
// Assert navigation action
assertEquals("movies/", navController.currentBackStackEntry?.destination?.route)
}
@Test
fun testMovieDetailsNavigation() {
// Create a test NavController
val navController = TestNavHostController(composeTestRule.activity)
val screens = Screens(navController)
// Setup your Composable with the TestNavHostController
composeTestRule.setContent {
screens.movieDetails(42)
}
// Perform navigation
screens.movieDetails(42)
// Assert navigation action
assertEquals("movies/42", navController.currentBackStackEntry?.destination?.route)
}
}
Screens class I'm testing:
package com.romahduda.movies30.navigation
import androidx.navigation.NavHostController
import com.romahduda.movies30.util.Constants.MOVIES_SCREEN
class Screens(navController: NavHostController) {
val movies: () -> Unit = {
navController.navigate("movies/"){
popUpTo(MOVIES_SCREEN) {inclusive = true}
}
}
val movieDetails: (Int) -> Unit = {movieId ->
navController.navigate("movies/$movieId")
}
}
I created TestNavGraph for this test but it didn't resolved the problem