Skip to content

Artifact for Testing #137

Open
Open
@sockeqwe

Description

@sockeqwe

The Problem:
To unit test a function that returns an instance of ChangeState<S> we need to "unwrap" it to get to the next state value by calling changeState.reduce(currentState). Not a big deal, but some repetitive boilerplate if you have to do that all the time for all of your tests. Also ChangeState is not meant to be compared directly with via equal with a real State like Assert.assertEquals(expectedState, changeState) (expectedState is of type State and changeState of type ChangeState).

Maybe we should provide an additional artifact to provide a more convenient way to unit test functions that involve ChangeState. Could be just a simple extension function like ChangeState.test(currentState) { newState -> ... } where you can do some assertions inside the lambda block on the newState object.

Or we could go wilder and define some more convenient API like:

 @Test
    fun `in loading state then load items and transition to content state`() = runBlocking {
        val items: List<Item> =  ...

        givenState<LoadingState, State>(LoadingState)
            .onEnter(::loadFirstPage) // loadFirstPage() is the function that we want to unit test
            .then(ShowContentState(notifications = items, currentPage = 1)) // Expected state

    }

UnitTestDSL.kt.txt

@gabrielittner @dmo60 do you thing that would be useful?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions