如何在模拟课程中调用特定的lambda



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

@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)和Next Call ReserveBadcasedevice不起作用。我检查了测试中的device Interactor,并且演示者是一个参考,演示者有需要值(WorkerID和DeviceContition),但是方法ReserveBadcccedevice并未称为所有内容。

当使用三重lambda函数的方法使用类别时,如何测试我的主持人方法储备库库库库?还是如何正确模拟设备互动器,该设备互动器将被称为第一个LAMDA?

device Interactor中的方法如下:

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

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

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())

相关内容

  • 没有找到相关文章

最新更新