在 Android 上测试 Kotlin 流

测试与 通信的单元或模块的方式取决于被测对象是将流用作输入还是输出。

  • 如果被测对象观察流,您可以在假依赖项中生成流,并可在测试中控制这些流。
  • 如果单元或模块公开流,您可以在测试中读取并验证流发出的一个或多个项目。

创建伪造的生产者

当被测对象是流的使用者时,一种常见的测试方法是用伪造的实现替换生产者。例如,给定一个在生产环境中观察从两个数据源获取数据的仓库的类

the subject under test and the data layer
图 1. 被测对象和数据层。

为了使测试具有确定性,您可以将仓库及其依赖项替换为始终发出相同伪造数据的伪造仓库

dependencies are replaced with a fake implementation
图 2. 依赖项被伪造的实现替换。

要在流中发出预定义的一系列值,请使用 flow 构建器

class MyFakeRepository : MyRepository {
    fun observeCount() = flow {
        emit(ITEM_1)
    }
}

在测试中,此伪造仓库会被注入,替换实际实现

@Test
fun myTest() {
    // Given a class with fake dependencies:
    val sut = MyUnitUnderTest(MyFakeRepository())
    // Trigger and verify
    ...
}

既然您可以控制被测对象的输出,您可以通过检查其输出来验证它是否正常工作。

在测试中断言流发射

如果被测对象公开一个流,则测试需要对数据流的元素进行断言。

假设上一个示例中的仓库公开一个流

repository with fake dependencies that exposes a flow
图 3. 带有伪造依赖项并公开流的仓库(被测对象)。

在某些测试中,您只需要检查流中的第一个发射或有限数量的项目。

您可以通过调用 first() 来消费流的第一个发射。此函数会等待直到接收到第一个项目,然后向生产者发送取消信号。

@Test
fun myRepositoryTest() = runTest {
    // Given a repository that combines values from two data sources:
    val repository = MyRepository(fakeSource1, fakeSource2)

    // When the repository emits a value
    val firstItem = repository.counter.first() // Returns the first item in the flow

    // Then check it's the expected item
    assertEquals(ITEM_1, firstItem)
}

如果测试需要检查多个值,调用 toList() 会导致流等待源发出所有值,然后将这些值作为列表返回。这仅适用于有限数据流。

@Test
fun myRepositoryTest() = runTest {
    // Given a repository with a fake data source that emits ALL_MESSAGES
    val messages = repository.observeChatMessages().toList()

    // When all messages are emitted then they should be ALL_MESSAGES
    assertEquals(ALL_MESSAGES, messages)
}

对于需要更复杂项目集合或不返回有限数量项目的数据流,您可以使用 Flow API 来选择和转换项目。以下是一些示例

// Take the second item
outputFlow.drop(1).first()

// Take the first 5 items
outputFlow.take(5).toList()

// Takes the first item verifying that the flow is closed after that
outputFlow.single()

// Finite data streams
// Verify that the flow emits exactly N elements (optional predicate)
outputFlow.count()
outputFlow.count(predicate)

测试期间的连续收集

如前一个示例所示,使用 toList() 收集流会在内部使用 collect(),并暂停直到整个结果列表准备好返回。

要在导致流发出值的操作和对已发出值的断言之间交错,您可以在测试期间持续从流中收集值。

例如,考虑以下要测试的 Repository 类,以及一个伴随的伪造数据源实现,它有一个 emit 方法可以在测试期间动态生成值

class Repository(private val dataSource: DataSource) {
    fun scores(): Flow<Int> {
        return dataSource.counts().map { it * 10 }
    }
}

class FakeDataSource : DataSource {
    private val flow = MutableSharedFlow<Int>()
    suspend fun emit(value: Int) = flow.emit(value)
    override fun counts(): Flow<Int> = flow
}

在测试中使用此伪造时,您可以创建一个收集协程,它将持续从 Repository 接收值。在此示例中,我们将它们收集到一个列表中,然后对其内容执行断言

@Test
fun continuouslyCollect() = runTest {
    val dataSource = FakeDataSource()
    val repository = Repository(dataSource)

    val values = mutableListOf<Int>()
    backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
        repository.scores().toList(values)
    }

    dataSource.emit(1)
    assertEquals(10, values[0]) // Assert on the list contents

    dataSource.emit(2)
    dataSource.emit(3)
    assertEquals(30, values[2])

    assertEquals(3, values.size) // Assert the number of items collected
}

因为这里 Repository 公开的流永远不会完成,所以收集它的 toList 调用永远不会返回。在 TestScope.backgroundScope 中启动收集协程可确保在测试结束前取消该协程。否则,runTest 将一直等待其完成,导致测试停止响应并最终失败。

请注意,此处收集协程使用了 UnconfinedTestDispatcher。这确保了收集协程是立即启动的,并在 launch 返回后准备好接收值。

使用 Turbine

第三方 Turbine 库为创建收集协程提供了方便的 API,以及用于测试流的其他便利功能

@Test
fun usingTurbine() = runTest {
    val dataSource = FakeDataSource()
    val repository = Repository(dataSource)

    repository.scores().test {
        // Make calls that will trigger value changes only within test{}
        dataSource.emit(1)
        assertEquals(10, awaitItem())

        dataSource.emit(2)
        awaitItem() // Ignore items if needed, can also use skip(n)

        dataSource.emit(3)
        assertEquals(30, awaitItem())
    }
}

有关更多详细信息,请参阅 库的文档

测试 StateFlow

StateFlow 是一个可观察的数据持有者,可以收集它以随着时间流观察它所持有的值。请注意,这个值流是合并的,这意味着如果值快速设置到 StateFlow 中,StateFlow 的收集器不保证会接收到所有中间值,只保证接收到最新的值。

在测试中,如果您牢记合并,您可以像收集任何其他流一样收集 StateFlow 的值,包括使用 Turbine。在某些测试场景中,尝试收集并断言所有中间值可能是可取的。

但是,我们通常建议将 StateFlow 视为数据持有者,并断言其 value 属性。这样,测试就可以在给定时间点验证对象的当前状态,并且不依赖于是否发生合并。

例如,考虑这个 ViewModel,它从 Repository 收集值,并通过 StateFlow 将它们暴露给 UI

class MyViewModel(private val myRepository: MyRepository) : ViewModel() {
    private val _score = MutableStateFlow(0)
    val score: StateFlow<Int> = _score.asStateFlow()

    fun initialize() {
        viewModelScope.launch {
            myRepository.scores().collect { score ->
                _score.value = score
            }
        }
    }
}

Repository 的伪造实现可能如下所示

class FakeRepository : MyRepository {
    private val flow = MutableSharedFlow<Int>()
    suspend fun emit(value: Int) = flow.emit(value)
    override fun scores(): Flow<Int> = flow
}

当使用此伪造测试 ViewModel 时,您可以从伪造中发出值以触发 ViewModelStateFlow 中的更新,然后断言更新后的 value

@Test
fun testHotFakeRepository() = runTest {
    val fakeRepository = FakeRepository()
    val viewModel = MyViewModel(fakeRepository)

    assertEquals(0, viewModel.score.value) // Assert on the initial value

    // Start collecting values from the Repository
    viewModel.initialize()

    // Then we can send in values one by one, which the ViewModel will collect
    fakeRepository.emit(1)
    assertEquals(1, viewModel.score.value)

    fakeRepository.emit(2)
    fakeRepository.emit(3)
    assertEquals(3, viewModel.score.value) // Assert on the latest value
}

使用 stateIn 创建的 StateFlow

在上一节中,ViewModel 使用 MutableStateFlow 来存储 Repository 中的流发出的最新值。这是一种常见模式,通常通过使用 stateIn 运算符以更简单的方式实现,该运算符将冷流转换为热 StateFlow

class MyViewModelWithStateIn(myRepository: MyRepository) : ViewModel() {
    val score: StateFlow<Int> = myRepository.scores()
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), 0)
}

stateIn 运算符有一个 SharingStarted 参数,它决定何时激活并开始消费底层流。像 SharingStarted.LazilySharingStarted.WhileSubscribed 这样的选项经常在视图模型中使用。

即使您在测试中断言 StateFlowvalue,您也需要创建一个收集器。这可以是一个空收集器

@Test
fun testLazilySharingViewModel() = runTest {
    val fakeRepository = HotFakeRepository()
    val viewModel = MyViewModelWithStateIn(fakeRepository)

    // Create an empty collector for the StateFlow
    backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
        viewModel.score.collect {}
    }

    assertEquals(0, viewModel.score.value) // Can assert initial value

    // Trigger-assert like before
    fakeRepository.emit(1)
    assertEquals(1, viewModel.score.value)

    fakeRepository.emit(2)
    fakeRepository.emit(3)
    assertEquals(3, viewModel.score.value)
}

其他资源