Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Callback 在kotlin中,如何模拟封装回调的挂起函数?_Callback_Kotlin_Mocking_Mockito_Kotlinx.coroutines - Fatal编程技术网

Callback 在kotlin中,如何模拟封装回调的挂起函数?

Callback 在kotlin中,如何模拟封装回调的挂起函数?,callback,kotlin,mocking,mockito,kotlinx.coroutines,Callback,Kotlin,Mocking,Mockito,Kotlinx.coroutines,假设有一个带有回调的接口: interface SomeInterface { fun doSomething(arg: String, callback: (Exception?, Long) -> Unit) } 我将其扩展为如下的挂起函数: suspend fun SomeInterface.doSomething(arg: String): Long = suspendCoroutine { cont -> this.doSomething(arg) { e

假设有一个带有回调的接口:

interface SomeInterface {
    fun doSomething(arg: String, callback: (Exception?, Long) -> Unit)
}
我将其扩展为如下的挂起函数:

suspend fun SomeInterface.doSomething(arg: String): Long = suspendCoroutine { cont ->
    this.doSomething(arg) { err, result ->
        if (err == null) {
            cont.resume(result)
        } else {
            cont.resumeWithException(err)
        }
    }
}
@Test
fun checkService() {
    runBlocking {
        val myService = mock<SomeInterface>()
        whenever(myService.doSomething(anyString())).thenReturn(1234L)
        val result = myService.doSomething("")
        assertEquals(result, 1234L)
    }
}
我想在测试中模拟一下,但我失败了。 理想情况下,我希望使用如下内容:

suspend fun SomeInterface.doSomething(arg: String): Long = suspendCoroutine { cont ->
    this.doSomething(arg) { err, result ->
        if (err == null) {
            cont.resume(result)
        } else {
            cont.resumeWithException(err)
        }
    }
}
@Test
fun checkService() {
    runBlocking {
        val myService = mock<SomeInterface>()
        whenever(myService.doSomething(anyString())).thenReturn(1234L)
        val result = myService.doSomething("")
        assertEquals(result, 1234L)
    }
}
如何模拟这样的挂起函数? 如果不可能使用类似的语法,如何使用所需的参数使模拟回调,从而使在整个代码中使用的挂起变量在测试期间返回所需的结果

更新: 当它是一个扩展函数时,似乎是不可能的。根据mockito的评论,我推断这是因为扩展仅仅是一个静态函数,而mockito不具备这个功能

当suspend函数是成员函数时,它会按照我的原始语法正常工作

以下是一些演示代码的要点:

我建议您在测试中使用,这对协同程序更为友好

要模拟协同程序,可以使用
coEvery
返回,如下所示:

val interf = mockk<SomeInterface>()
coEvery { a.doSomething(any()) } returns Outcome.OK
val-interf=mockk()
coEvery{a.doSomething(any())}返回output.OK
当您需要时

在。。。。一定要回来

方法被挂起,使用mockito可以使用:

  • 使用这个库
  • 模拟您的对象,将其命名为myObject(myObject具有名为isFoo的挂起方法)
然后:


实际上,回调并不需要匹配器,而是隐式延续参数all
suspend fun
s在类文件级别声明。试着为它提供一个空的
Continuation
无论何时(myService.doSomething(anyString(),any(Continuation.class))
都不会编译,也不会
any()
。另外,如果我对第二个参数使用
any()
,那么
。然后返回(1234L)
不起作用,因为它需要
Unit
,而不是
Long
如果我在那里添加第二个参数,我基本上是在模拟原始接口调用,而不是suspend函数。我不介意模拟原始接口,只要suspend变量工作并在测试期间以我想要的方式响应。我只是不知道如何实现。啊耶s、 我忘了它。扩展的乐趣实际上是编译类中的一个
公共静态
Java方法。您将无法对其进行模拟。但是,有一些方法可以模拟原始方法,而不仅仅是
然后返回