简体   繁体   English

如何在specs2中检查对象的许多属性?

[英]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. 我不知道如何组合specs2匹配器以允许您检查对象的多个属性。 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): 可以,但是在单个属性(c.name和c.domain)上使用匹配器,而不是最终结果会更好(因为后一种情况根本不能描述失败):

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

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: 前提是您编写了自己的Cookie匹配器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM