Skip to content

view testing

Florian Schuster edited this page Nov 14, 2021 · 6 revisions

a consumer or view that binds its ui to a controller, can be tested by accessing the Controller.stub(). once accessed, the Controller is stubbed and cannot be un-stubbed.

when stubbed, Controller.mutate() and Controller.reduce() are not executed.

  • use ControllerStub.dispatchedActions : List<Action> to verify if view bindings trigger the correct actions
  • use ControllerStub.emitState(State) to verify if Controller.state is correctly bound to a view
@Test
fun valueButtonClickTriggersCorrectAction() {
    // given
    val controller = //create ValueController
    val view = View(controller.apply { stub() })
    
    // when
    onView(withId(R.id.setValueButton)).perform(click())
    
    // then
    assertEquals(
        Action.SetValue(value = 3), 
        controller.stub().dispatchedActions.last()
    )
}

@Test
fun stubbedStateUpdatesTextViewText() {
    // given
    val expectedValue = 42
    val controller = //create ValueController
    val view = View(controller.apply { stub() })
    
    // when
    controller.stub.emitState(State(counterValue = expectedValue))
    
    // then
    onView(withId(R.id.valueTextView))
        .check(matches(withText("$testValue")))
}

examples

Clone this wiki locally