gpt4 book ai didi

kotlin - 如何使用 Spock (groovy) 测试 kotlin 挂起函数

转载 作者:行者123 更新时间:2023-12-02 19:49:12 24 4
gpt4 key购买 nike

我有以下 kotlin 挂起函数:

suspend fun registerNewTransaction(transaction: String): String

我正在尝试通过 Spock 来测试它,但我还没有找到在不指定 Continuation<? super String> 的情况下调用这个 kotlin 挂起函数的方法参数,因为 Groovy 将 kotlin 容器类识别为 Java 类。我想知道是否可以从其他基于 JVM 的语言调用挂起函数,否则我将不得不使用另一个测试库,例如 JUnit。

最佳答案

您可以模拟该 Continuation 对象,kotlin 正在将其编译到方法调用中。这段代码对我有用:

// mocked continuation
def continuation = Mock(Continuation) {
getContext() >> Mock(CoroutineContext)
}

// method call which should be tested
subjectUnderTest.suspendableMethod(someParameter, continuation)

关于kotlin - 如何使用 Spock (groovy) 测试 kotlin 挂起函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58575985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com