android - 将 LiveDataTestUtil 与 Kotest 结合使用

标签 android android-livedata android-architecture-components kotest

我一直在阅读LiveDatatestUtil.kt provided as part of the Android Architecture Components Samples ,我一直在尝试找出如何测试 Events尽可能在 Kotest 内使用它。主要是因为(目前)Kotest 不提供 LiveData 测试功能。是否有一种惯用的方法来测试事件(基于 LiveData),即基于已发布的 util?

最佳答案

我的解决方案是将以下内容添加到实用程序中:

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
infix fun <T, U : T> LiveData<Event<T>>.shouldBeTriggered(
    expected: U
) {
    val value = this.getOrAwaitValue()
    value.getContentIfNotHandled() shouldBe expected
}

这样就可以使用事件 shouldBeTriggered withValue 作为测试。

关于android - 将 LiveDataTestUtil 与 Kotest 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65151363/

相关文章:

android - 具有多个发件人的 GCM

android-architecture-components - View.findNavController() 与 Fragment.findNavController()

java - panoramagl 热点将移至下一个全景

android - 如何使用观察者使用 ViewPager 中当前项目位置的值来验证 EditText 中的值?

android - MutableLiveData 中 setValue() 和 postValue() 的区别

android - LiveData 与 StateFlow : Should we switch from Live data to State Flow?

android - 分页库 - 无法停止加载项目

android - LiveData - 将 SingleLiveEvent 与转换结合使用

android - getLocationUpdates() 在 4.0 模拟器上给出非法参数异常

java - 将 JSON "subarray"传递给另一个 Activity