mvrx-mocking / com.airbnb.mvrx.mocking / mockSingleViewModel

mockSingleViewModel

fun <V : MockableMavericksView, Args : Parcelable, S : MavericksState> V.mockSingleViewModel(viewModelReference: KProperty1<V, MavericksViewModel<S>>, defaultState: S, defaultArgs: Args?, mockBuilder: SingleViewModelMockBuilder<V, Args, S>.() -> Unit): MockBuilder<V, Args>

Define state values for a MavericksView that should be used in tests. This is for use with MockableMavericksView.provideMocks when the view has a single view model.

In the mockBuilder lambda you can use MockBuilder.args to define mock arguments that should be used to initialize the view and create the initial view model state. Use SingleViewModelMockBuilder.state to define complete state objects.

It is recommended that the "Default" mock state for a view represent the most canonical, complete form of the View. For example, all data loaded, with no empty or null instances.

Other mock variations should test alterations to this "default" state, with each variant testing a minimal difference (ideally only one change). For example, a variant may test that a single property is null or empty.

This pattern is encouraged because:

  1. The tools for defining mock variations are designed to enable easy modification of the default
  2. Each mock variant is only one line of code, and is easily maintained
  3. Each variant tests a single edge case

Parameters

V - The type of Mavericks view that is being mocked

viewModelReference - A reference to the view model that will be mocked. Use the "::" operator for this - "MyFragment::myViewModel"

defaultState - An instance of your State that represents the canonical version of your view. It will be the basis for your tests.

defaultArgs - If your view takes arguments you must provide an instance of those arguments here to be used as the default value for your mocks. If your view has no arguments, pass null (and use Nothing as the type). For example, if you use Fragments this would be the arguments that you initialize the Fragment with. If you use the Mavericks.KEY_ARG key to pass a single Parcelable class to your Fragment you can pass that class here, and it will automatically be wrapped in a Bundle with the Mavericks.KEY_ARG key. Otherwise you can pass a Bundle and it will be passed along as-is to your Fragment when it is created.

mockBuilder - A lambda where the SingleViewModelMockBuilder DSL can be used to specify additional mock variants.

See Also

mockTwoViewModels

mockNoViewModels