Android SDK类上Kotlin扩展函数的单元测试

Android SDK类上Kotlin扩展函数的单元测试,android,unit-testing,kotlin,kotlin-android-extensions,kotlin-extension,Android,Unit Testing,Kotlin,Kotlin Android Extensions,Kotlin Extension,Kotlin扩展功能非常强大。但我如何对它们执行单元测试呢?尤其是Android SDK提供的类(例如上下文、对话框) 我在下面提供了两个例子,如果有人可以分享我如何对它们进行单元测试,或者如果我真的想对它们进行单元测试,我是否需要以不同的方式编写它们 fun Context.getColorById(colorId: Int): Int { if (Build.VERSION.SDK_INT >= 23) return ContextCompat.getColor

Kotlin扩展功能非常强大。但我如何对它们执行单元测试呢?尤其是Android SDK提供的类(例如上下文、对话框)

我在下面提供了两个例子,如果有人可以分享我如何对它们进行单元测试,或者如果我真的想对它们进行单元测试,我是否需要以不同的方式编写它们

fun Context.getColorById(colorId: Int): Int {
    if (Build.VERSION.SDK_INT >= 23)
        return ContextCompat.getColor(this, colorId)
    else return resources.getColor(colorId)
}


任何建议都会有帮助。谢谢

目前我在Android类上测试扩展函数的方式是模仿Android类。我知道,这不是一个最佳的解决方案,因为它模拟被测试的类,并且需要关于函数如何工作的特定知识(模拟时总是这样),但是由于扩展函数是作为静态函数在内部实现的,我想这是可以接受的,直到有人想出更好的方法

作为一个例子,考虑<代码> JsonArray < /代码>类。我们定义了一个用于接收最后一项索引的扩展函数:

fun JSONArray.lastIndex() = length() - 1
相应的测试(使用测试框架和)如下所示

@RunWith(JUnitPlatform::class)
object JsonExtensionTestSpec : Spek({

    given("a JSON array with three entries") {
        val jsonArray = mock<JSONArray> {
            on { length() } doReturn 3
        }

        on("getting the index of the last item") {
            val lastIndex = jsonArray.lastIndex()

            it("should be 2") {
                lastIndex shouldBe 2
            }
        }
    }

    given("a JSON array with no entries") {
        val jsonArray = mock<JSONArray>({
            on { length() } doReturn 0
        })

        on("getting the index of the last item") {
            val lastIndex = jsonArray.lastIndex()

            it("should be -1") {
                lastIndex shouldBe -1
            }
        }
    }
})
@RunWith(JUnitPlatform::class)
对象JsonExtensionTestSpec:Spek({
给定(“一个包含三个条目的JSON数组”){
val jsonArray=mock{
关于{length()}doReturn 3
}
关于(“获取最后一项的索引”){
val lastIndex=jsonArray.lastIndex()
它(“应该是2”){
最后一个索引应该是2
}
}
}
给定(“没有条目的JSON数组”){
val jsonArray=mock({
关于{length()}doReturn 0
})
关于(“获取最后一项的索引”){
val lastIndex=jsonArray.lastIndex()
它(“应该是-1”){
lastIndex应该是-1
}
}
}
})

函数的困难在于,它们在内部也使用Android类。不幸的是,我现在没有解决方案。

您好,我认为您不需要对这些函数进行单元测试。首先,从android资源中获取颜色,而不依赖于您的应用程序代码。第二个显示errorDialog,为什么不使用Espresso或Robotium或其他UI测试框架来检查它是否正确显示?谢谢piotrek。代码只是一个例子。这个问题的主要要点是探讨在需要测试扩展函数中是否存在某些逻辑的情况下,如何对扩展函数进行单元测试。这是无法实现的,还是我错过了什么?谢谢。我也在找。你找到它了吗@Elye?
@RunWith(JUnitPlatform::class)
object JsonExtensionTestSpec : Spek({

    given("a JSON array with three entries") {
        val jsonArray = mock<JSONArray> {
            on { length() } doReturn 3
        }

        on("getting the index of the last item") {
            val lastIndex = jsonArray.lastIndex()

            it("should be 2") {
                lastIndex shouldBe 2
            }
        }
    }

    given("a JSON array with no entries") {
        val jsonArray = mock<JSONArray>({
            on { length() } doReturn 0
        })

        on("getting the index of the last item") {
            val lastIndex = jsonArray.lastIndex()

            it("should be -1") {
                lastIndex shouldBe -1
            }
        }
    }
})