我尝试使用Specs2编写一些测试异步API的单元测试。根据用户指南,通过在同步代码中添加.await
来检查Futures应该很容易。然而,虽然这适用于我已经测试过的一些事情,但在下面的例子中检查序列的长度并不像预期的那样工作:
"A Seq" should {
"be empty when nothing was added" in {
Seq[String]() must have length(0) // compiles and runs correctly
Seq[String]() must be empty // compiles and runs correctly
}
"be empty when nothing was added async" in { implicit ee: ExecutionEnv =>
Future{ Seq[String]() } must have.await length(0) // doesn't compile
Future{ Seq[String]() } must be.await empty // doesn't compile
Future{ Seq[String]() } must have length(0).await // doesn't compile
Future{ Seq[String]() } must be empty.await // doesn't compile
}
}
使用Specs2执行此操作的惯用方法是什么?
答案 0 :(得分:3)
这里有specs2语法和类型推断的限制。您需要编写以下内容:
Future(Seq[String]()) must beEmpty[Seq[String]].await
基本上,您不能将await
与be + matcher
语法一起使用,而必须注释beEmpty
匹配器。