我一直在阅读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/