单元测试验证传递给要调用的函数

Unit Test Verify a Function Passed to be Called

假设我有这个函数(用 Kotlin 编写):

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

是否可以验证(在单元测试中)showChatAssignerFunction 在 assignee 为 null 或为空时调用?谢谢大家!

当然可以:

@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))
}