The Artima Developer Community
Sponsored Link

ScalaTest/ScalaUtils Forum
Using a HavePropertyMatcher for collection elements?

2 replies on 1 page. Most recent reply: Aug 10, 2011 12:06 AM by Ori Regev

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Ori Regev

Posts: 2
Nickname: ori
Registered: Jun, 2003

Using a HavePropertyMatcher for collection elements? Posted: Aug 9, 2011 8:17 AM
Reply to this message Reply
Advertisement
I'm a ScalaTest newbie and I'm trying to understand if it's possible to define the following spec using the built-in matchers (in FeatureSpec) 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.

thanks,
Ori


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Using a HavePropertyMatcher for collection elements? Posted: Aug 9, 2011 12:14 PM
Reply to this message Reply
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)

Ori Regev

Posts: 2
Nickname: ori
Registered: Jun, 2003

Re: Using a HavePropertyMatcher for collection elements? Posted: Aug 10, 2011 12:06 AM
Reply to this message Reply
Thanks for the update Bill.

By (ab)using HavePropertyMatcher I was able to get a bit closer the English sentence I wanted (not perfect though):

books should have (bookTitled ("Moby Dick"))

We want our QA guys to write the specs and therefore clauses like _.title == "..." are a bit too technical :)

Ori

Flat View: This topic has 2 replies on 1 page
Topic: junit-addons Previous Topic   Next Topic Topic: Missing method in JMockCycle

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use