如何在mock类中调用特定的lambda?

时间:2017-09-25 11:45:19

标签: unit-testing mockito kotlin

我有下一个带参数的文本案例:

@Test
fun lostClick() {
        //setup parameters

        val func0 : (Unit) -> Unit = { println("fun0")}
        val func1 : (String) -> Unit = { println("fun1")}
        val func2 : (Int) -> Unit = { println("fun2")}

        whenever(deviceInteractor.reserveBadCaseDevice( //equals `when`
                eq(workerId),
                eq(DeviceCondition.LOST),
                eq(func0),
                eq(func1),
                eq(func2))).then({ func0.invoke(Unit) })

        presenter.lostClick()

        //view reaction
        verify(viewState).showLoad(true)
//        verify(viewState).setButtonGiveEnabled(true) // if func0 called this verify works
//        verify(viewState).setButtonTakeEnabled(false)
//        verify(viewState).setButtonCrashEnabled(false)
//        verify(viewState).setButtonLostEnabled(false)
//        verify(viewState).showLoad(false)
    }

但这不起作用。 func0没有被调用。在我的演示者这个方法看到这个:

fun lostClick() {
    reserveBadCaseUser(DeviceCondition.LOST)
}

private fun reserveBadCaseUser(condition: DeviceCondition) {
        showLoad(true)

        //condition == DeviceCondition.LOST
        deviceInteractor.reserveBadCaseDevice(mCurrentWorker?.id!!, condition,
                {
                    clearCurrentDevice()
                    validateButtonView()
                    showLoad(false)
                },
                {
                    showLoad(false)
                    viewState.showErrorToast(it)
                },
                {
                    showLoad(false)
                    viewState.showErrorToast(it)
                })
    }

调用showLoad(true),然后调用reserveBadCaseDevice无效。我在test和presenter中检查了deviceInteractor是一个引用,presenter有需要的值(workerId和DeviceCondition),但方法reserveBadCaseDevice没有调用所有东西。

如果使用具有三重lambda函数的方法的类,那么如何测试我的presenter方法reserveBadCaseUser?或者如何正确地模拟deviceInteractor,这将被称为第一个lamda?

deviceInteractor中的

方法如下所示:

fun reserveBadCaseDevice(workerId: Int,
                             condition: DeviceCondition,
                             result: (Unit) -> Unit,
                             errorText: (String) -> Unit,
                             errorId: (Int) -> Unit) {
        val data = ReservedWorkerData(
                DeviceState.TAKE,
                condition, null,
                workerId)

        val call = apiService.postAsyncReserveDevice(data, prefManager.getPrefToken())
        call.enqueue(object : Callback<Void> {
            override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
                response?.let {
                    if (response.isSuccessful) {
                        result.invoke(Unit)
                    } else {
                        errorText(apiErrorUtil.parse(response).message)
                    }
                }
            }

            override fun onFailure(call: Call<Void>?, t: Throwable?) {
                errorId(apiErrorUtil.parse(t).message)
            }
        })
    }

1 个答案:

答案 0 :(得分:0)

事实证明,解决方案非常简单。感谢一个好的社区,你真的很有帮助!

doAnswer{
            println("invoke")
            val args = it.getArguments()

            (args[2] as (Unit) -> Unit).invoke(Unit)
        }.whenever(deviceInteractor).reserveBadCaseDevice(
                eq(workerId),
                eq(DeviceCondition.LOST),
                any(),
                any(),
                any())