ScalaTest - 如何检查序列的所有项目是否与谓词匹配?

ScalaTest - how to check that all items of sequence match the predicate?

有没有办法显示更好的错误信息?例如,下面的代码

it should "check that items satisfy predicate" in {
  List(1,2,3,4,5,6).forall(x => x < 5) should equal (true)
}

只给我以下错误,没有任何描述:

"false did not equal true"

谓词不仅可以是least表达式,还可以是任何其他函数。 least 我找到了 https://www.scalatest.org/user_guide/using_matchers#inspectorShorthands.

您可以将问题从

"check that items satisfy predicate"

"check that all the items in the list are smaller than 5"

然后这个答案就更有意义了

想要更有意义的回答,可以拨打fail,赞

fail("I've got a bad feeling about this.")

您还可以通过多种方式使用断言,阅读 this 了解更多信息。示例:

assume(database.isAvailable, "The database was down again")
  List(1,2,3,4,5,6).foreach { _ should be < 5 }

can not only be least expression, but any other function

使用inspector尝试

forAll (xs) { x => assert(pred(x)) }

where pred: T => Boolean,输出索引和谓词失败的信息性错误消息,例如

- should check that items satisfy predicate *** FAILED ***
  forAll failed, because: 
    at index 4, pred(x) was false (HelloSpec.scala:41) 
  in List(1, 2, 3, 4, 5, 6) (HelloSpec.scala:41)