【问题标题】:Scalacheck nested forall throws "oneOf called on empty collection" on failure in Scalatest testScalacheck 嵌套 forall 在 Scalatest 测试失败时抛出“oneOf called on empty collection”
【发布时间】:2017-01-24 16:10:43
【问题描述】:

我有一个嵌套的 forAll 调用,它取决于前一个生成的值。根据生成器的定义,该值是一个不应为空的集合:

"test" in {
  val listGen: Gen[List[Int]] = Gen.listOfN(3, Gen.choose(0, 100))
  def intGen(list: List[Int]) = Gen.oneOf(list)
  implicit val arbList = Arbitrary(listGen)

  forAll { list: List[Int] =>
    forAll(intGen(list)) { int =>
      true should be(false)
    }
  }
}

而不是产生诸如“真不是假”之类的错误消息,这给了我:

IllegalArgumentException was thrown during property evaluation.
  Message: oneOf called on empty collection
  Occurred when passed generated values (
    arg0 = List() // 2 shrinks
)

我不知道为什么。这不是我期望(或理解)的测试失败消息...

如果我打印出生成的列表和整数,我会得到以下奇怪的输出:

List(72, 77, 8)
8
4
2
1
0
List(72)
72
36
18
9
4
2
1
0
List()

由于列表被截断,属性失败时整数被二除(但根据生成器定义,列表的大小应始终为 3!)

我正在使用 Scala 2.12.1,scalatest 3.0.1 abd scalacheck 1.13.4。

谢谢!

【问题讨论】:

    标签: scala scalatest scalacheck


    【解决方案1】:

    原来这种机制称为“收缩”,可以通过将这些隐式添加到范围中来禁用:

      def noShrink[T] = Shrink[T](_ => Stream.empty)
      implicit val intListNoShrink = noShrink[List[Int]]
      implicit val intNoShrink = noShrink[Int]
    

    这个链接很有帮助:https://github.com/scalatest/scalatest/issues/584

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2015-11-21
      • 2018-11-21
      • 1970-01-01
      相关资源
      最近更新 更多