Unit Test Verify a Function Passed to be Called

960 views Asked by At

Let's say i have this function (written in Kotlin):

fun determineBottomBarView(assignee: String?,
                           showChatAssignerFunction: () -> Unit,
                           showChatComposerFunction: () -> Unit,
                           hideChatComposerAndAssignerFunction: () -> Unit) {
    if (assignee.isNullOrEmpty()) {
        showChatAssignerFunction()
    } else {
        if (assignee.equals(localRequestManager.getUsername())) {
            showChatComposerFunction()
        } else {
            hideChatComposerAndAssignerFunction()
        }
    }
}

Is it possible to verify (in unit test) showChatAssignerFunction to be called when assignee is null or empty? Thank you all!

1

There are 1 answers

1
miensol On BEST ANSWER

Sure you can:

@Test
fun `just testing`() {
    var showedChatAssigner = false
    var showChatComposer = false
    var didHideChat = false

    determineBottomBarView(null,{ showedChatAssigner = true }, { showChatComposer = true }, { didHideChat = true })

    assertThat(showedChatAssigner, equalTo(true))
    assertThat(showChatComposer, equalTo(false))
    assertThat(didHideChat, equalTo(false))
}