Using PropertyProject for collection items in ScalaTest?

I've been using ScalaTest FeatureSpec for a couple of days now, and I'm trying to figure out if the following specification can be determined using the built-in mappings (and if not, how can I write a suitable custom mapper).

Suppose I have a Book class:

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 point out that the book list should contain a book called "Moby Dick". I would like to write something like:

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

I can’t understand from the docs and code if this can be expressed in ScalaTest. Has anyone encountered a similar situation?

+5
source share
2 answers

- . - :

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

:

def containElement[T](right: Matcher[T]) = new Matcher[Seq[T]] {
  def apply(left: Seq[T]) = {
    val matchResults = left map right
    MatchResult(
      matchResults.find(_.matches) != None,
      matchResults.map(_.failureMessage).mkString(" and "),
      matchResults.map(_.negatedFailureMessage).mkString(" and ")
    )
  }
}

ScalaTest Have :

val books = List(Book("Moby Dick", "Melville"),
                 Book("Billy Budd", "Melville"), 
                 Book("War and Peace", "Tolstoy"))

books should containElement(have('title("Moby Dick")))
books should containElement(have('title("Billy Budd"), 'author("Melville")))
books should containElement(have('title("War and Peace"), 'author("Melville")))

, :

title "Moby Dick" " ", (Moby Dick, Melville), title "Billy Budd" " " ", ( , ), " ", " ", ( , ).

and or, not ..

+2

All Articles