【问题标题】:ScalaTest assert and matchersScalaTest 断言和匹配器
【发布时间】:2016-05-14 20:29:42
【问题描述】:

我在我的 Scala Play 项目中使用了 ScalaTest。但是我这里有个问题,什么时候使用普通的assert(xxx === yyy),什么时候使用像xxx should be yyy这样的ScalaTest匹配器。

我个人更喜欢使用 assert,因为它简单干净。也可以利用 ScalaTest 的 TypedCheckTrippleEquals 支持,但匹配器不能。

对于matchers,到目前为止我只发现了一件事matchers可以做但不能断言,那就是Array(1, 2) should equal (Array(1, 2))成功了。

不太确定推荐哪个,还有其他匹配器可以做的更多吗?否则很乐意使用断言。

【问题讨论】:

  • 好问题。文档here 真的很好。

标签: scala scalatest matcher


【解决方案1】:

以下是我喜欢 ma​​tchers 的一些东西:

1) 检查范围内的数字

sevenDotOh should equal (6.9 +- 0.2)

2) 检查长度

result should have length 3

3) 检查类型

result1 shouldBe a [Tiger] 

4) 检查一个(或多个)元素是否属于集合

List(1, 2, 3, 4, 5) should contain oneOf (5, 7, 9)
List(1, 2, 3) should contain (2)

5) 各种酷炫的测试

List(1, 2, 3) shouldBe sorted

如果您还没有阅读此documentation,请阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2019-03-10
    • 2016-01-05
    • 2014-11-16
    • 2018-05-25
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多