我一直在通读LiveDatatestUtil.kt provided as part of the Android Architecture Components Samples,并且一直在尽可能地找出如何在Kotest中使用它来测试Events。主要是因为(现在) Kotest不提供LiveData测试功能。有没有一种惯用的方法来测试事件(基于LiveData),那就是基于发布的util?
发布于 2020-12-05 06:03:51
我的解决方案是将以下内容添加到util中:
@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作为测试。
https://stackoverflow.com/questions/65151363
复制相似问题