unit-testingscalascalatest

Using a HavePropertyMatcher for collection elements in ScalaTest?


I've been using ScalaTest's FeatureSpec for a couple of days now and I'm trying to understand if it's possible to define the following spec using the built-in matchers (and if not, how I can write a suitable custom matcher).

Suppose I have the class Book:

case class Book(val title: String, val author: String)

and in my test I have a List of books:

val books = List(Book("Moby Dick", "Melville"))

Now, I would like to specify that the books list should contain a book with the title "Moby Dick". I would like to write something like:

books should contain (value with title "Moby Dick")  

I can't seem to figure out from the docs and code if it's possible to express this requirement in ScalaTest. Has anyone ran into a similar situation?


Solution

  • Not currently, though you will be able to do something like this very soon in the future. What you can do now is something like:

    books.exists(_.title == "Moby Dick") should be (true)