Skip to content

MvRx Testing

Eli Hart edited this page Dec 11, 2019 · 1 revision

The mvrx-testing artifact can be added as a test dependency to provide additional tooling when writing tests for MvRx ViewModels and screens.

MvRx Test Rule

The MvRxTestRule class can be used as a JUnit rule to assist with configuring ViewModels for each test class.

class MyTestClass {

    @get:Rule
    val mvrxRule = MvRxTestRule()

    @Test fun ...
}

This rule allows you to configure behavior such as:

  • Forcing RxJava schedulers to be immediate, so state changes happen synchronously
  • Forcing ViewModel subscriptions to happen even when a Fragment is not STARTED
  • Changing the debugMode setting of each ViewModel
  • Changing the mock behavior of ViewModels so that state changes can be prevented or overridden

See the kdoc of MvRxTestRule for further details.