在 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.
目标是如果匹配器失败,则在错误消息中包含有关具有失败属性的对象的相关上下文。