将 LiveDataTestUtil 与 Kotest 一起使用
Using LiveDataTestUtil with Kotest
我一直在 Kotest 中尽可能通读 LiveDatatestUtil.kt provided as part of the Android Architecture Components Samples, and I have been trying to work out how to test Events。主要是因为(现在)Kotest 不提供 LiveData 测试功能。是否有一种基于已发布实用程序的惯用方法来测试事件(基于 LiveData)?
我的解决方案是将以下内容添加到实用程序中:
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
infix fun <T, U : T> LiveData<Event<T>>.shouldBeTriggered(
expected: U
) {
val value = this.getOrAwaitValue()
value.getContentIfNotHandled() shouldBe expected
}
然后允许使用 event shouldBeTriggered withValue
作为测试。
我一直在 Kotest 中尽可能通读 LiveDatatestUtil.kt provided as part of the Android Architecture Components Samples, and I have been trying to work out how to test Events。主要是因为(现在)Kotest 不提供 LiveData 测试功能。是否有一种基于已发布实用程序的惯用方法来测试事件(基于 LiveData)?
我的解决方案是将以下内容添加到实用程序中:
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
infix fun <T, U : T> LiveData<Event<T>>.shouldBeTriggered(
expected: U
) {
val value = this.getOrAwaitValue()
value.getContentIfNotHandled() shouldBe expected
}
然后允许使用 event shouldBeTriggered withValue
作为测试。