Joseph Thomas-Kerr
Joseph Thomas-Kerr

Reputation: 339

How do you check a number of properties of an object in specs2?

I can't figure out how to combine specs2 matchers to allow you to check multiple properties of an object. For example, I have a collection of objects and I want to assert that at least one of them matches a number of constraints.

This works, but it would be much nicer to use matchers on the individual properties (c.name and c.domain), rather than the final result (since the latter case is not at all descriptive about the failure):

  response.cookies.exists(c => 
        c.name.exists(_.equals("PLAY_SESSION")) && 
        ".mydomain.com".equals(c.domain)
  ) must beTrue

Upvotes: 1

Views: 575

Answers (1)

Eric
Eric

Reputation: 15557

You can try this

response.cookies must contain { c: Cookie => 
  c must (haveName("PLAY_SESSION") and haveDomain(".mydomain.com"))
}

Provided that you wrote your own Cookie matchers:

def haveName(name: String): Matcher[Cookie] = { c: Cookie =>
  (c.name.exists(_.equals(name)), s"$c doesn't contain the name $name")
}

Upvotes: 2

Related Questions