androidx.lifecycle.viewmodel.testing


Classes

ViewModelScenario

ViewModelScenario provides API to start and drive a ViewModel's lifecycle state for testing.

Cmn

Top-level functions summary

CreationExtras

Creates a default instance of CreationExtras.

Cmn
android
N
CreationExtras

Creates a default instance of CreationExtras pre-configured with all keys required to use SavedStateHandle, with the specified defaultArgs as the DEFAULT_ARGS_KEY.

android
inline ViewModelScenario<VM>
<VM : ViewModel> viewModelScenario(
    creationExtras: CreationExtras,
    noinline initializer: CreationExtras.() -> VM
)

Creates a ViewModelScenario using a given VM class as key, an initializer as a ViewModelProvider.Factory and a creationExtras as default extras.

Cmn
inline ViewModelScenario<VM>
<VM : ViewModel> viewModelScenario(
    factory: ViewModelProvider.Factory,
    creationExtras: CreationExtras
)

Creates a ViewModelScenario using a given VM class as key, an factory and a creationExtras as default extras.

Cmn

Top-level functions

DefaultCreationExtras

fun DefaultCreationExtras(): CreationExtras

Creates a default instance of CreationExtras.

DefaultCreationExtras

fun DefaultCreationExtras(defaultArgs: Bundle): CreationExtras

Creates a default instance of CreationExtras pre-configured with all keys required to use SavedStateHandle, with the specified defaultArgs as the DEFAULT_ARGS_KEY.

This function sets up the instance with:

inline fun <VM : ViewModel> viewModelScenario(
    creationExtras: CreationExtras = DefaultCreationExtras(),
    noinline initializer: CreationExtras.() -> VM
): ViewModelScenario<VM>

Creates a ViewModelScenario using a given VM class as key, an initializer as a ViewModelProvider.Factory and a creationExtras as default extras.

You should access the ViewModel instance using ViewModelScenario.viewModel, and clear the ViewModelStore using ViewModelScenario.close.

Example usage:

viewModelScenario { MyViewModel(parameters) }
.use { scenario ->
val vm = scenario.viewModel
// Use the ViewModel
}
Parameters
<VM : ViewModel>

The reified ViewModel class to be created.

creationExtras: CreationExtras = DefaultCreationExtras()

Additional data passed to the Factory during a ViewModel creation.

noinline initializer: CreationExtras.() -> VM

A Factory function to create a ViewModel.

inline fun <VM : ViewModel> viewModelScenario(
    factory: ViewModelProvider.Factory,
    creationExtras: CreationExtras = DefaultCreationExtras()
): ViewModelScenario<VM>

Creates a ViewModelScenario using a given VM class as key, an factory and a creationExtras as default extras.

You should access the ViewModel instance using ViewModelScenario.viewModel, and clear the ViewModelStore using ViewModelScenario.close.

Example usage:

val myFactory: ViewModelProvider.Factory = MyViewModelFactory()
viewModelScenario<MyViewModel>(myFactory)
.use { scenario ->
val vm = scenario.viewModel
// Use the ViewModel
}
Parameters
<VM : ViewModel>

The reified ViewModel class to be created.

factory: ViewModelProvider.Factory

A Factory to create a ViewModel.

creationExtras: CreationExtras = DefaultCreationExtras()

Additional data passed to the Factory during a ViewModel creation.