kotlin mockk 测试要求延迟的时间长度

问题描述

我想测试一个 delay() 是否在协程中被调用了 x 天。而且我不确定以下测试是如何工作的...

@Test
fun 'wait 5 days'() {

    ...

    runBlocking {
        service.main(testEnv)
    }

    val slot = slot<Long>()
    coVerify(exactly = 1) { testEnv.timeoutProvider.sleep(capture(slot)) }
    assert(slot.captured == 5.days.inMilliseconds.toLong())
}

testEnv 是一个包含 timeoutProvider 的对象。服务使用此 timeoutProvider 来调用延迟。然后我想验证已调用的延迟是否为 5 天。为什么这个测试有效?

open class TimeOutProvider {

    suspend fun sleep(timeInSeconds: Long) {
        delay(timeInSeconds * 1000L)
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)