Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kasia-kittel/f65c9eb895b1234c3691103dd0a299d2 to your computer and use it in GitHub Desktop.
Save kasia-kittel/f65c9eb895b1234c3691103dd0a299d2 to your computer and use it in GitHub Desktop.

Scalatest matchers doesn't work with implicit conversion. Example:

"testing" when {

    implicit def stringToInt(s:String):Int = 123            //not used
    implicit def intToString(i:Int):String = "oneTwoThree"  //not used

    "implicit" should {

      "just work " in {
        val s: String = "oneTwoThree"
        s should equal (123)
      }
    }
  }

A way to convert String to Int for the purspose of this test is implementing Equality[String].

implicit def stringToIntEquality: Equality[String] = new Equality[String] {
    override def areEqual(a: String, b: Any): Boolean = b match {
        case bInt: Int => if (bInt == 123)  true else false
        case _ => false
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment