迁移主协程规则

发布于 2025-01-11 13:23:05 字数 710 浏览 4 评论 0原文

我想创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野却迷人 2025-01-18 13:23:05

这是我到目前为止所拥有的,但我没有 cleanupTestCoroutines 的解决方案,

class MainCoroutineRule(
    private val dispatcher: TestDispatcher = StandardTestDispatcher()
) : TestWatcher() {
    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(dispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
    }
}

请随意使用更好的解决方案编辑/修改它

This is what I have so far, but I don't have a solution for cleanupTestCoroutines

class MainCoroutineRule(
    private val dispatcher: TestDispatcher = StandardTestDispatcher()
) : TestWatcher() {
    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(dispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
    }
}

Feel free to edit/modify this with a better solution

无人问我粥可暖 2025-01-18 13:23:05

我的解决方案并不完美,但这是我最好的。
根据迁移提示,< code>runTest 不支持替换 cleanupTestCoroutines()
相反,它们支持更好的结构化并发。
因此您无需担心 cleanupTestCoroutines()

所以,接下来是我的 MainCoroutineExtension。

@OptIn(ExperimentalCoroutinesApi::class)
class MainCoroutineExtension(
    private val testDispatcher: TestDispatcher = StandardTestDispatcher(),
    val testScope: TestScope = TestScope(testDispatcher),
) : BeforeEachCallback, AfterEachCallback {
    override fun beforeEach(context: ExtensionContext) {
        Dispatchers.setMain(testDispatcher)
    }

    override fun afterEach(context: ExtensionContext) {
        Dispatchers.resetMain()
    }

}

@OptIn(ExperimentalCoroutinesApi::class)
fun MainCoroutineExtension.runTest(
    block: suspend TestScope.() -> Unit
) = this.testScope.runTest {
    block()
}

我使用 JUnit 5 和新的协程。
所以我无法使用 runBlockingTest 因为它很快就会被弃用。
如果您打算迁移您的协程,这将对您有所帮助

My solution will not be perfect, but it was my best.
According to migration tips, runTest does not support replacement for cleanupTestCoroutines().
Instead of that, they support better structured concurrency.
So you don't need to worry about cleanupTestCoroutines().

So, follow is my MainCoroutineExtension.

@OptIn(ExperimentalCoroutinesApi::class)
class MainCoroutineExtension(
    private val testDispatcher: TestDispatcher = StandardTestDispatcher(),
    val testScope: TestScope = TestScope(testDispatcher),
) : BeforeEachCallback, AfterEachCallback {
    override fun beforeEach(context: ExtensionContext) {
        Dispatchers.setMain(testDispatcher)
    }

    override fun afterEach(context: ExtensionContext) {
        Dispatchers.resetMain()
    }

}

@OptIn(ExperimentalCoroutinesApi::class)
fun MainCoroutineExtension.runTest(
    block: suspend TestScope.() -> Unit
) = this.testScope.runTest {
    block()
}

I use JUnit 5 and new coroutine.
So I couldn't use runBlockingTest because it will be deprecated soon.
If you have plan to migrate your coroutine, It will be helpful to you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文