One can use have to check if property equals to a value.
Is there some way to check the property not for equality, but to check if it satisfies a matcher?
Following compiles, but unsurprisingly it does not work, as the property is tested for equality with the matcher value.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class MainTest extends AnyFlatSpec with Matchers {
case class Book(title: String, author: List[String], pubYear: Int)
"Something" should "work" in {
val book = Book("Programming in Scala", List("Odersky", "Spoon", "Venners"), 2008)
book should have (
Symbol("title") ("Programming in Scala"),
Symbol("pubYear") (be >= 2006 and be <= 2010)
)
}
}
Consider Inside
which allows you to assert statements about case class properties using pattern matching:
"Something" should "work" in {
inside (book) { case Book(title, _, pubYear) =>
title should be ("Programming in Scala")
pubYear should (be >= 2008 and be <= 2010)
}
}