我的可组合函数
@Composable
fun MyScreen(
myViewModel: MyViewModel,
navController: NavController
){}
我的viewodel类
@HiltViewModel
class MyViewModel @Inject constructor(
val myUseCases: MyUseCases
): ViewModel()
可组合的UI测试类
@HiltAndroidTest
class MyScreenKtTest{
@get:Rule(order = 0)
val composeTestRule = createComposableRule()
@get:Rule(order = 1)
val hiltTestRule = HiltAndroidRule(this)
lateinit var navController : TestNavHostController
@Before
fun init(){
hiltTestRule.inject()
}
@Test
fun DemoTestFunction() {
composeTestRule.setContent {
navController = TestNavHostController(LocalContext.current)
MyScreen(myViewModel = myViewModel,navController = navController)
}
composeTestRule.onNodeWithText("My name").assertIsDisplayed()
}
}
错误是
无法创建类MyViewModel的示例
如何在可组合UI测试中注入视图模型?
1条答案
按热度按时间nqwrtyyt1#
您在init()块中提到了composeTestRule.setContent{}