Hi all :android-wave: , I am trying test compose n...
# compose
a
Hi all 👋 , I am trying test compose navigation as mentioned here. https://developer.android.com/develop/ui/compose/navigation#testing I am getting the following error.
Copy code
Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
Please help thank you color
solved 1
Complete test code
Copy code
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.navigation.compose.ComposeNavigator
import androidx.navigation.testing.TestNavHostController
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test

internal class MyNavHostTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    private lateinit var navHostController: TestNavHostController

    @Before
    fun setupAppNavHost() {
        composeTestRule.setContent {
            navHostController = TestNavHostController(
                context = LocalContext.current,
            )
            navHostController.navigatorProvider.addNavigator(
                navigator = ComposeNavigator(),
            )

            MyNavHost(
                navHostController = navHostController,
            )
        }
    }

    @Test
    fun startDestinationIsHome() {
        Assert.assertEquals(
            "home",
            navHostController.currentBackStackEntry?.destination?.route,
        )
    }
}
c
I don't suggest using navhost and nav controller
👎🏼 1
a
This is from Android Compose Navigation library
c
Their document is very bad.
Offen, I don't follow them.
You can just use mutable state to navigate through pages. take a look at my project. https://github.com/Free-Web-Movement/zz/blob/main/app/src/main/java/io/github/freewebmovement/zz/ui/TabView.kt
I was driven crazy by their bad documents at first before I found the mutable states are better at ui controller.
c
@Abhimanyu do your other tests work that use
createComposeRule
?
c
404
not found - probably a private repo 🙂
a
Oops. Yeah 😅
This test works.
c
I just implemented a navigation test in my project. check it out, the tests work without issues. also check your imports. not sure why there would be any
hilt
invocations when using the
androidx.activity.ComponentActivity
https://github.com/chrimaeon/compose-playground/blob/21fe99638ee5136b8a5fc089a4568[…]s/android/compose/screen/SharedElementTransitionScreenShould.kt
w
It sounds like you're calling hiltViewModel somewhere in the composition tree.
💯 1
a
w
But you don't have the test set up for Hilt.
👍🏼 1
a
It sounds like you're calling hiltViewModel somewhere in the composition tree.
Yes, that would be the case as I am testing the
NavHost
. Thank You all. gratitude thank you