迁移主协程规则
我想创建 MainCoroutineRule。
但 TestCoroutineScope 自 1.6.0 起已弃用。
我该如何迁移它?
下面
class MainCoroutineRule(
val dispatcher: TestCoroutineDispatcher =
TestCoroutineDispatcher()
): TestWatcher(), TestCoroutineScope by TestCoroutineScope(dispatcher) {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description) {
super.finished(description)
cleanupTestCoroutines()
Dispatchers.resetMain()
}
}
也是我的 MainCoroutineRule,我想在 JUnit5 上使用它。 我应该怎么做才能迁移 TestWatcher?
I want create MainCoroutineRule.
But TestCoroutineScope deprecated since 1.6.0.
How can I migrate it?
Below is my MainCoroutineRule
class MainCoroutineRule(
val dispatcher: TestCoroutineDispatcher =
TestCoroutineDispatcher()
): TestWatcher(), TestCoroutineScope by TestCoroutineScope(dispatcher) {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description) {
super.finished(description)
cleanupTestCoroutines()
Dispatchers.resetMain()
}
}
also, I want to use it on JUnit5.
What should I do to migrate TestWatcher?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我到目前为止所拥有的,但我没有
cleanupTestCoroutines
的解决方案,请随意使用更好的解决方案编辑/修改它
This is what I have so far, but I don't have a solution for
cleanupTestCoroutines
Feel free to edit/modify this with a better solution
我的解决方案并不完美,但这是我最好的。
根据迁移提示,< code>runTest 不支持替换
cleanupTestCoroutines()
。相反,它们支持更好的结构化并发。
因此您无需担心
cleanupTestCoroutines()
。所以,接下来是我的 MainCoroutineExtension。
My solution will not be perfect, but it was my best.
According to migration tips,
runTest
does not support replacement forcleanupTestCoroutines()
.Instead of that, they support better structured concurrency.
So you don't need to worry about
cleanupTestCoroutines()
.So, follow is my MainCoroutineExtension.