首页 文章

具有未来结果的Specs2数据表

提问于
浏览
1

我正在努力解决以下问题:我有一个返回 Future[Result] 的方法,其中 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.sequenceSeq[Future[Result]] 转换为 Future[Seq[Result]] ,然后运行测试 .

有理智的做法吗?

1 回答

  • 2

    没有简单的方法可以做到这一点,你需要解构和重建一些东西

    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
    }
    

相关问题