Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何模拟/匹配kotlin方法签名中的lambda_Unit Testing_Kotlin_Mockito_Kotlintest - Fatal编程技术网

Unit testing 如何模拟/匹配kotlin方法签名中的lambda

Unit testing 如何模拟/匹配kotlin方法签名中的lambda,unit-testing,kotlin,mockito,kotlintest,Unit Testing,Kotlin,Mockito,Kotlintest,我在以下表格中有一些代码: @Language("SQL") val someSql = """ SELECT foo FROM bar WHERE foo = :foo """ return session.select(some, mapOf("foo" to foo)) { MyObject( foo = it.string("foo"), ) }.firstOrNull() 使用com.github.andrew

我在以下表格中有一些代码:

@Language("SQL")
val someSql = """
    SELECT foo
    FROM bar 
    WHERE foo = :foo
    """
return session.select(some, mapOf("foo" to foo)) {
    MyObject(
            foo = it.string("foo"),
    )
}.firstOrNull()
使用com.github.andrewoma.kwery.core中的以下内容。注意方法签名中的lambda:

fun <R> select(@Language("SQL") sql: String,
               parameters: Map<String, Any?> = mapOf(),
               options: StatementOptions = defaultOptions,
               mapper: (Row) -> R): List<R> 
在我的例子中,function2不是一个真正的映射器,它不是我试图模拟的对象的eq,它从不匹配,也从不调用模拟


那么,我应该在会话模拟中放置什么,选择而不是上面代码中的eq(function2)来返回MyObject对象呢?

我认为您只需要指定映射器在设置会话模拟时预期返回的类型-在您的情况下,看起来是
Function1

val会话=mock{
在{select(sql=anyString(),parameters=anyMap(),options=any(),mapper=any())}上返回(listOf(MyObject(foo=“test foo”))
}
val function2: (Row) -> Int = mock {
    onGeneric { invoke(any()) }.thenReturn(MyObject(foo="test-foo"))
}

val session = mock<Session> {
    on { select(sql = any(), parameters = any(), options = any(), mapper = eq(function2))}.thenReturn(listOf(MyObject(foo="test-foo")))
} 
val session = mock<Session> {
    on { select(sql = anyString(), parameters = anyMap(), options = any(), mapper = any<Function1<Row, MyObject>>())}.thenReturn(listOf(MyObject(foo="test-foo")))
}