在这个 Kotlin 代码中:
async {
lateinit var testResult: TestResult
val startTime = Date()
try {
withContext(Dispatchers.IO) {
unitTest.getTestToRunAsync()?.invoke { result ->
testResult = result
doOnTestCompleted(testInfo, testResult, startTime, Date())
}
}
} catch (exception: Exception) {
testResult = TestResult("Exception: ${exception.message}")
}
}
我想调用 doOnTestCompleted,它的定义如下:
private suspend fun doOnTestCompleted(
testInfo: TestInfo,
testResult: TestResult,
startTime: Date,
endTime: Date,
copyTestResults: Boolean = true
) {
}
但我不能调用 doOnTestCompleted 因为调用它的 lambda 主体不是协程。getTestToRunAsync 的定义是:
fun getTestToRunAsync(): (suspend (testToRunAsync: (resultCallback: TestResult) -> Unit) -> Unit)? {
return this.testToRunAsync
}
如何调用 doOnTestCompleted?