scalaunit-testingtype-inferencespecs2

Specs2 spec fails to compile after upgrade to latest version


I have just upgraded Specs2 on my project and now some specs won't compile and it isn't clear why they're not, here's the spec:

"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")
  }

}

(The whole file can be seen here)

And the compiler says:

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 { ^

And while I understand what it is saying, it doesn't make any sense given I am returning ok or failure and I'm covering all cases on my match.

Any idea what could be wrong here?


Solution

  • The compiler is trying to find the common type of the 2 match branches. The first line is using ok which is a MatchResult and the second line is using failure which is returning a Result. Their only common type is Product with Serializable.

    The fix is simply to use the opposite value of ok which is ko:

    factory.validate(connection) match {
      case Failure(e) => ok("Connection successfully rejected")
      case Success(c) => ko("should not have come here")
    }
    

    You could also write

    import org.specs2.execute._
    
    ...
    
    factory.validate(connection) match {
      case Failure(e) => Success("Connection successfully rejected")
      case Success(c) => failure("should not have come here")
    }
    

    There is however no success(message: String) method available to match the corresponding failure. I will add it to the next specs2 version for better symmetry.