【问题标题】:ScalaTest - How to verify a collection contains a sub classScalaTest - 如何验证集合是否包含子类
【发布时间】:2016-10-01 04:35:58
【问题描述】:

使用 ScalaTest,可以很简单地测试对象是否属于特定类,例如:

myPet shouldBe a [Dog]

但我想做的是验证某个基类型的集合是否至少具有一个特定的子类型。我想语法可能看起来像这样,但它不起作用:

myPets should contain a [Cat]

我可以使用“exists”和“shouldBe true”来获得相同的结果,但它的表现力不够。

myPets.exists(_.isInstanceOf[Cat]) shouldBe true

这是我必须做的还是 ScalaTest 有我不知道的功能?

谢谢

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    atLeast(1, myPets) shouldBe a [Cat] 应该可以工作,我相信(目前无法测试,仅基于来自 http://www.scalatest.org/user_guide/using_matchers#inspectorShorthands 的示例)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-20
      • 2014-11-14
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2020-02-02
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多