我刚刚在我的项目中升级了 Specs2,现在有些规范无法编译,不清楚为什么不能编译,这是规范:
"fail validation if a connection is disconnected" in {
val connection = factory.create
awaitFuture(connection.disconnect)
factory.validate(connection) match {
case Failure(e) => ok("Connection successfully rejected")
case Success(c) => failure("should not have come here")
}
}
(整个文件可见 here )
编译器说:
could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Product with Serializable] "fail validation if a connection is disconnected" in { ^
虽然我明白它在说什么,但考虑到我返回ok
或failure
并且我涵盖了比赛中的所有情况,这没有任何意义.
知道这里可能出了什么问题吗?
最佳答案
编译器正在尝试查找 2 个匹配分支的公共(public)类型。第一行使用 ok
,它是一个 MatchResult
,第二行使用 failure
,它返回一个 Result
。它们唯一常见的类型是可序列化的产品
。
修复方法只是使用 ok
的相反值,即 ko
:
factory.validate(connection) match {
case Failure(e) => ok("Connection successfully rejected")
case Success(c) => ko("should not have come here")
}
你也可以这样写
import org.specs2.execute._
...
factory.validate(connection) match {
case Failure(e) => Success("Connection successfully rejected")
case Success(c) => failure("should not have come here")
}
但是没有success(message: String)
方法可用于匹配相应的failure
。我会将其添加到下一个specs2版本中以获得更好的对称性。
关于unit-testing - 升级到最新版本后 Specs2 规范无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287769/