【发布时间】:2021-11-18 09:59:23
【问题描述】:
我目前有以下功能:
fun createMask(mask : String){
val ssnField : mywidgets.SSNField = findViewById (R.id.editTextText)
ssnField.hint = mask
}
为了对此进行单元测试,我想将 createMask 中不可测试的代码包装到一个闭包中。 (不可测试的代码是难以在单元测试中实例化和执行的视图层逻辑。)这是我想要在伪代码中做的事情:
createMask(closure, mask : String){
closure = mask // closure function returns pointer to property (depending on closure return type, might need to use setter: closure.set(mask))
}
使用上述内容,调用者会这样做:
fun caller(){
createMask((){
val ssnField : mywidgets.SSNField = findViewById (R.id.editTextText)
return ssnField.hint
}, "xxx-xx-xxx")
}
在 kotlin 中用伪代码表达的内容是如何工作的?
【问题讨论】:
标签: unit-testing kotlin closures