首页 > 解决方案 > 是否可以对具有无参数且无结果的函数类型参数的函数进行断言?

问题描述

我试图弄清楚以下几点:

我有一个模拟方法,在这个模拟类中有一个函数,该函数的参数不带任何内容并返回 void,类似这样的“.()”在 Kotlin 中是什么意思?

fun invokeStuff(values: (MyParametersBuilder.() -> Unit)) {
        // do something and extract values
    }

MyParametersBuilder类有一些自己的属性设置器。

所以当我调用它时,我会这样做

MyClass().invokeStuff{
     setEnableFlagA(true)
}

所以现在使用Mockito我遇到了这个问题,我无法模拟 invokeStuff 的结果,因为我不知道如何去做,甚至不知道它是否可能。

基本上,我不能这样做:

when(myClass.invokeStuff(any(MyParametersBuilder)).thenReturn... <- it says this method doesn't exist

两者都不

when(myClass.invokeStuff{any(Boolean::class.java)}.thenReturn... <- compiles but doesnt match anything

也不

when(myClass.invokeStuff{}.thenReturn... <- doesn't match either

知道我该怎么做吗?我使用 kotlin 和 mockito。

标签: kotlinmockito

解决方案


推荐阅读