使用 shareIn() 测试 Kotlin Flows

问题描述

我正在尝试测试使用 shareInturbine 的 Flow,但我有点不明白为什么我的测试失败以及如何修复它。

class MyTest {

    private val scope = Coroutinescope(dispatchers.Default)
    private val mutableSharedFlow = MutableSharedFlow<Int>()

    @Test
    fun succeeds() = runBlocking {
        val sharedFlow = mutableSharedFlow

        sharedFlow.test {
            expectNoEvents()
            mutableSharedFlow.emit(3)
            expect(expectItem()).toBe(3)
        }
    }

    @Test
    fun fails() = runBlocking {
        val sharedFlow = mutableSharedFlow
            .shareIn(scope,started = SharingStarted.WhileSubscribed())

        sharedFlow.test {
            expectNoEvents()
            mutableSharedFlow.emit(3)
            expect(expectItem()).toBe(3)
        }
    }
}

在这些测试中,第一个 succeeds() 测试运行良好,但是一旦我在 shareIn 测试中包含 fails(),测试就会因超时而失败:

Timed out waiting for 1000 ms
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms
    (Coroutine boundary)
    at app.cash.turbine.ChannelBasedFlowturbine$expectEvent$2.invokeSuspend(Flowturbine.kt:238)
    at app.cash.turbine.ChannelBasedFlowturbine$withTimeout$2.invokeSuspend(Flowturbine.kt:206)
    at app.cash.turbine.ChannelBasedFlowturbine.expectItem(Flowturbine.kt:243)

我应该如何测试使用 shareIn 的流?

解决方法

我不知道您为什么决定使用带有 Dispatchers.Default 的范围,如下所示:

...
private val scope = CoroutineScope(Dispatchers.Default)
...

对于测试,只需使用 Dispatchers.Unconfined,因为它会立即在当前线程上执行协程,而这正是您在那里所需要的。

...
private val scope = CoroutineScope(Dispatchers.Unconfined)
...

因此,在应用上述更改后,您的两个测试均成功通过。

您可以在 here 中找到我的示例项目。