在 scalatest 中是否有更好的方法来检查数字是否应在范围内?

Fre*_*ind 3 scala scalatest

假设我想检查结果(整数)是否应该>=4<=15,我可以在 ScalaTest 测试中编写这样的断言:

assert(num >= 4)
assert(num <= 15)
Run Code Online (Sandbox Code Playgroud)

它有效,但我认为还可以改进。有没有更好的方法在 Scalatest 中检查它?

man*_*nub 5

像这样的东西会起作用(使用Matchers):

val beWithin4and5 = be >= 4 and be <= 5

value should beWithin4And5
Run Code Online (Sandbox Code Playgroud)

  • 并且需要一行括号:`value should (be &gt;= 4 and be &lt;= 5)` (2认同)