【问题标题】:Using LiveDataTestUtil with Kotest将 LiveDataTestUtil 与 Kotest 一起使用
【发布时间】:2020-12-04 22:03:51
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    我的解决方案是在 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 作为测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多