简体   繁体   English

请帮助 - Jetpack Compose 测试 - Hilt - ViewModel - Repository

[英]Please help - Jetpack Compose Testing - Hilt - ViewModel - Repository

I am trying to learn how to test with Jetpack Compose and I'm feeling lost.我正在尝试学习如何使用 Jetpack Compose 进行测试,但我感到迷茫。 I'm not sure what I am doing wrong.我不确定我做错了什么。 I want to test the MainScreen, but it is nested in a ScreenNavigation() and needs a ViewModel and a NavController.我想测试 MainScreen,但它嵌套在 ScreenNavigation() 中,需要一个 ViewModel 和一个 NavController。 I'm really confused how this works.我真的很困惑这是如何工作的。 It is telling me it can't find the activity.它告诉我它找不到活动。 Im not sure what to do in the AndroidManifest file.我不确定在 AndroidManifest 文件中要做什么。 Any help is very much appreciated!很感谢任何形式的帮助!

Error: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samm.brewerysearch.test/androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity};错误:android.content.ActivityNotFoundException:无法找到显式活动 class {com.samm.brewerysearch.test/androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared?您是否在 AndroidManifest.xml 中声明了此活动,或者您的意图与其声明的不匹配?

<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>


<application
    android:name="com.samm.brewerysearch.BrewApplication"
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Bloomberg"
    tools:targetApi="31">
    <activity
        android:name="com.samm.brewerysearch.MainActivity"
        android:exported="true"
        android:theme="@style/Theme.Bloomberg">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
@HiltAndroidTest
@UninstallModules(AppModule::class)
class MainScreenTest {

    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeRule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setUp() {
        hiltRule.inject()

        composeRule.setContent {
            val navController = rememberNavController()
            
            BreweryTheme {
                NavHost(
                    navController = navController,
                    startDestination = Screens.MainScreen.name
                ){
                    composable(Screens.MainScreen.name){
                        MainScreen(
                            navController = navController,
                            mainViewModel = hiltViewModel(),
                            search = Constants.DEFAULT_CITY
                        )
                    }
                }
            }
        }
    }


    @Test
    fun myTest(){
        composeRule.onNodeWithText(Constants.DEFAULT_CITY).assertIsDisplayed()
    }
}

Add these rule to test your screen and mock viremodel添加这些规则来测试你的屏幕和模拟 viremodel

  @get:Rule
     val initRule: MockitoRule = MockitoJUnit.rule() 

    @get:Rule
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()
    
    @Mock
    lateinit var mainViewModel: MainViewModel

you can initialize your navcontroller by您可以通过以下方式初始化您的导航控制器

this.navController  = rememberNavController()

furthermore ther's no need to add @HiltAndroidTest annotation to your class, replace this annotation with @RunWith(MockitoJUnitRunner::class)此外,无需将@HiltAndroidTest注释添加到您的 class 中,将此注释替换为@RunWith(MockitoJUnitRunner::class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM