单元测试Kotlin协同程序延迟
我试图单元测试使用delay()
的Kotlin协同程序。 对于单元测试,我不在乎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) } }
我希望我可以从我的上下文的delay()
方法返回,而是调用我的scheduleResumeAfterDelay()
方法,我不知道如何将其委派给默认调度程序。