我正在努力解决以下问题: 我有一个返回 Future [Result] 的方法,其中结果是我想要检查specs2中的数据表行。
据我所知,以下代码每次都会阻止并等待结果可用。
def getDataForInput(input: String) : Future[Result]
def myTest =
"input" | "expectedResult" |>
"a" ! 123 |
"b" ! 0 | {
(input, result) => getDataForInput input must( beEqualTo(result).await )
}
通常情况下,我想异步拨打所有电话,然后使用 Future.sequence 将 Seq [Future [Result]] 转换为 Future [Seq [结果]] 然后运行测试。
有没有理智的方法来做到这一点?
答案 0 :(得分:2)
没有 easy 方法可以做到这一点,你需要解构和重构一些事情
type RowResult = (Seq[String], Result)
val table =
"input" | "expectedResult" |
"123" ! 123 |
"0" ! 1 |
"0" ! 0
// get a Future containing all rows and results
val results: Future[List[RowResult]] = table.rows.toList.traverseU { row =>
getDataForInput(row.t1).map { i =>
(row.showCells, (i must beEqualTo(row.t2)).toResult)
}
}
// check the results
results must beSuccessfulTable(table.titles).await
这使用了一个自定义匹配器,它可以更好地显示表格
// the TextTable does an even display of columns
import org.specs2.text.TextTable
def beSuccessfulTable(titles: Seq[String]): Matcher[List[RowResult]] = { values: List[RowResult] =>
val overallSuccess = values.map(_._2).reduce(_ and _).isSuccess
val headers = if (overallSuccess) titles else Seq("")++titles++Seq("")
val table = TextTable(headers, values.map(resultLine.tupled))
(overallSuccess, table.show)
}
// helper method
def resultLine = (line: Seq[String], result: Result) => {
val message = if (result.isSuccess) "" else result.message
result.status +: line :+ message
}