Kotlin 扩展功能很棒。但是我怎么能对它们进行单元测试呢?尤其是那些 Android SDK 提供的类(例如 Context、Dialog)。
我在下面提供了两个示例,如果有人可以分享我如何对它们进行单元测试,或者如果我真的想对它们进行单元测试,是否需要以不同的方式编写它们。
fun Context.getColorById(colorId: Int): Int {
if (Build.VERSION.SDK_INT >= 23)
return ContextCompat.getColor(this, colorId)
else return resources.getColor(colorId)
}
和
fun Dialog.setupErrorDialog(body : String, onOkFunc: () -> Unit = {}): Dialog {
window.requestFeature(Window.FEATURE_NO_TITLE)
this.setContentView(R.layout.dialog_error_layout)
(findViewById(R.id.txt_body) as TextView).text = body
(findViewById(R.id.txt_header) as TextView).text = context.getString(R.string.dialog_title_error)
(findViewById(R.id.txt_okay)).setOnClickListener{
onOkFunc()
dismiss()
}
return this
}
任何建议都会有所帮助。谢谢!