Isolated Fragment testing

How to initialize activity ViewModel without launching activity and test the Fragment using FragmentScenario in isolated testing.

Hello,

Dependency injection is the best way to do this. Koin, Hilt etc. allow you to inject your ViewModels instead of directly using the view model providers. This allows you to instantiate an instance of the shared viewmodel you want to test, presumably with a certain state that may be required for your isolated fragment test, in the setup of your test. Then you run the scenario and go from there.

So if you were using Koin and setting up your viewmodel in a module

lateinit var yourViewModel: YourViewModel

fun setup(){
loadKoinModules(
module(override = true) {
viewModel {
yourViewModel = YourViewModel()
yourViewModel
}
})
yourViewModel.
}

Does that make sense?