ScalaTest:对属性进行任意检查

时间:2013-10-22 16:24:37

标签: scala scalatest

在ScalaTest中,是否有可能对任意属性表达断言,这些属性不仅仅对这些属性的值进行简单的相等比较?

例如,而不是:

 val list = List(1,2,3,4,5)
 list.length should be < 4             // Fails with the unhelpful error message "5 is not less than 4"

 case class Example(field: String)
 val obj = Example("TEST")
 obj.field should be allLowerCase      // Given "allLowerCase", this fails without any information about obj.

我想做以下事情:

  val list = List(1,2,3,4,5)
  list should have length < 4         // Fails with an error message containing the list

  obj should have ('field (allLowerCase))  // Fails with an error message containing obj.

如果匹配器失败,目标是在错误消息中包含有关具有失败属性的对象的相关上下文。

1 个答案:

答案 0 :(得分:0)

obj.field must beAllLowerCase

beAllLowerCase是你的匹配器。

相关问题