Closed Jaehwa-Noh closed 7 months ago
Personally I've always written JVM tests for ViewModels where you just instantiate them normally and only wrote Android tests for components using the VMs — Fragments, Activities, compose functions. Any reason you don't want to write AmphibiansViewModelTest
as a JVM test?
Thank you. I misunderstand the documentation in Unit test and UI test section. So I change the code like this, and it works very well.
class AmphibiansViewModelTest {
@get:Rule
val testDispatcher = TestDispatcherRule()
private lateinit var amphibiansViewModel: AmphibiansViewModel
@Before
fun injectHiltRule() {
amphibiansViewModel = AmphibiansViewModel(amphibiansInfoRepository = FakeAmphibiansInfoRepository())
}
@Test
fun amphibiansViewModel_GetAmphibiansInfo_GetAmphibiansListSuccess() {
assertEquals(
AmphibiansUiState.Success(FakeAmphibiansDataSource.getAmphibiansInfo),
amphibiansViewModel.amphibiansUiState.value
)
}
}
Hello, I'm trying to test ViewModel on Unit test. But, I can't use
@Inject
on ViewModel. I can't found any documentation about @HiltViewModel Unit test. How can I test it? Is there any documentation?I'm using compose.
My ViewModel
And unit test code.
This is error message.