Kotlin与supervisor的多发射协同路由失败
Kotlin与supervisor的多发射协同路由失败,kotlin,junit,kotlin-coroutines,mockk,Kotlin,Junit,Kotlin Coroutines,Mockk,工厂的列表应为 在IO调度程序上执行 执行所有工厂,即使它们崩溃 应该以某种方式记录碰撞(测试中不干净:errorRecord) 由于以下原因,测试失败: java.lang.AssertionError: Verification failed: call 1 of 1: Factory(#2).create(any())) was not called at io.mockk.impl.recording.states.VerifyingState.failIfNotPasse
工厂的列表应为
- 在IO调度程序上执行
- 执行所有工厂,即使它们崩溃
- 应该以某种方式记录碰撞(测试中不干净:errorRecord)
由于以下原因,测试失败:
java.lang.AssertionError: Verification failed: call 1 of 1: Factory(#2).create(any())) was not called
at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalVerify(API.kt:118)
at io.mockk.MockKKt.verify(MockK.kt:146)
at io.mockk.MockKKt.verify$default(MockK.kt:143)
以下是测试本身:
import io.mockk.Runs
导入io.mock.every
导入io.mock.just
导入io.mockk.mockk
导入io.mock.verify
导入kotlinx.coroutines.CoroutineExceptionHandler
导入kotlinx.coroutines.Dispatchers
导入kotlinx.coroutines.experimentalRoutinesAPI
导入kotlinx.coroutines.SupervisorJob
导入kotlinx.coroutines.launch
导入kotlinx.coroutines.test.runBlockingTest
导入org.junit.Test
@实验常规
类协同测试{
@试验
有趣的问题()=运行BlockingTest{
val test1=mockk{every{create(any())}抛出IllegalStateException()}
val test2=mockk{every{create(any())}刚刚运行}
val test3=mockk{every{create(any())}刚刚运行}
val test4=mockk{every{create(any())}刚刚运行}
var errorRecord:可丢弃?=null
val exceptionHandler=CoroutineExceptionHandler{},error->errorRecord=error}
启动(Dispatchers.IO+SupervisorJob()+exceptionHandler){
listOf(test1、test2、test3、test4).forEach{
启动{it.create(“test”)}
}
}
验证(正好=1){test1.create(any())}
验证(正好=1){test2.create(any())}
验证(正好=1){test3.create(any())}
验证(正好=1){test4.create(any())}
断言(errorRecord!=null)
}
接口工厂{
乐趣创建(数据:字符串)
}
}
有时它是成功的。(我猜在test1是最后一个调用的情况下)
有什么想法吗?在调用verify
之前,您似乎不会等待启动-ed协同程序的结束。您可能应该为coroutineScope
或with context
切换顶部launch
,而不是使用runBlockingTest
?当我执行您建议的更改时,我得到:java.lang.IllegalStateException:此作业尚未完成
运行BlockingTest
只确保当它返回时,所有的协程都将完成,但是验证
调用都在块内,因此无法保证^。看来runBlockingTest
可能会强制立即分派launch
,我的错(我有一段时间没有使用协同程序测试了,对不起)