mvrx-mocking / com.airbnb.mvrx.mocking / MavericksMock

MavericksMock

class MavericksMock<V : MavericksView, Args : Parcelable>

Defines a unique variation of a View's state for testing purposes.

A view is represented completely by:

  1. The arguments used to initialize it
  2. The State classes of all ViewModels used by the View

For proper mocking, the View MUST NOT reference data from any other sources, such as static singletons, dependency injection, shared preferences, etc. All of this should be channeled through the ViewModel and State. Otherwise a mock cannot deterministically and completely be used to test the View.

An exception is Android OS level View state, with things such as scroll and cursor positions. These are not feasible to track in state, and are generally independent and inconsequential in testing.

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

Types

Type

enum class Type

There are a set of standard mock variants that all Views have. Beyond that users can define custom mock variants.

Properties

args

val args: Args?

Returns the arguments that should be used to initialize the Mavericks view. If null, the view models will be initialized purely with the mock states instead.

forInitialization

val forInitialization: Boolean

If true, this mock tests the view being created either from arguments or with the viewmodels' default constructor (when args is null).

isDefaultInitialization

val isDefaultInitialization: Boolean

isDefaultState

val isDefaultState: Boolean

isForProcessRecreation

val isForProcessRecreation: Boolean

name

val name: String

states

val states: List<MockState<V, *>>

The State to set on each ViewModel in the View. There should be a one to one match.

type

val type: MavericksMock.Type

Functions

equals

fun equals(other: Any?): Boolean

hashCode

fun hashCode(): Int

stateForViewModelProperty

fun stateForViewModelProperty(property: KProperty<*>, existingViewModel: Boolean): MavericksState?

Find a mocked state value for the given view model property.