unit-testing - 升级到最新版本后 Specs2 规范无法编译

标签 unit-testing scala compiler-construction type-inference specs2

我刚刚在我的项目中升级了 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 { ^

虽然我明白它在说什么,但考虑到我返回okfailure并且我涵盖了比赛中的所有情况,这没有任何意义.

知道这里可能出了什么问题吗?

最佳答案

编译器正在尝试查找 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/

相关文章:

javascript - JSMock 的奇怪问题

django - 带有查询参数的 APITest 是否与普通 url 不同?

compiler-construction - 基于云的编译器是否可行?

c - BNF 文法中的递归

java - 如何使用模拟覆盖@Produces

c++ - 如何强制 Google 测试用例最后运行

scala - 将 Slick 与无形的 HList 一起使用

scala - 使用 scala akka spray can 对 http 服务器进行非常基本的测试时出错

scala - 有什么方法可以在最新的 SBT 版本中抑制来自 ScalaTest 的堆栈跟踪?

c - 最新版本的 TurboC 是什么?