我正在尝试对使用的Kotlin协程进行单元测试delay()
.对于单元测试我不关心delay()
,它只是减慢测试速度.我想以某种方式运行测试,这种方式在delay()
调用时实际上并没有延迟.
我尝试使用委托给CommonPool的自定义上下文来运行协同程序:
class TestUiContext : CoroutineDispatcher(), Delay {
suspend override fun delay(time: Long, unit: TimeUnit) {
// I'd like it to call this
}
override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) {
// but instead it calls this
}
override fun dispatch(context: CoroutineContext, block: Runnable) {
CommonPool.dispatch(context, block)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我可以从我的上下文delay()
方法返回,但是它调用我的scheduleResumeAfterDelay()
方法,我不知道如何将它委托给默认的调度程序.