【问题标题】:What's missing from this ScalaTest/ScalaCheck example?这个 ScalaTest/ScalaCheck 示例缺少什么?
【发布时间】:2015-05-10 14:00:05
【问题描述】:

我正在尝试 ScalaCheck,但无法弄清楚如何构建 ScalaTest 用户手册中的第一个示例。我很确定以下内容之前需要一些导入并包装在从某个库类扩展的类中。 page in the user's manual 说要“混入”PropertyChecks,但它没有给出将其混入的适当类的示例:

forAll { (n: Int, d: Int) =>

  whenever (d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

    val f = new Fraction(n, d)

    if (n < 0 && d < 0 || n > 0 && d > 0)
      f.numer should be > 0
    else if (n != 0)
      f.numer should be < 0
    else
      f.numer should be === 0

    f.denom should be > 0
  }
}

我一直在尝试各种组合,但到目前为止我得到的最好结果是这样的编译错误:

[info] Compiling 1 Scala source to .../target/scala-2.11/test-classes...
[error] .../src/test/scala/TestFraction.scala:14: value should is not a member of Int
[error]         f.numer should be > 0
[error]                 ^

什么是测试、导入和所有的完整源文件?

【问题讨论】:

    标签: scala scalatest scalacheck


    【解决方案1】:

    如果你在这里查看 scala test 的 github 存储库,你可以在这里找到一些额外的代码

    github source scalatest

    对于您的来源,这是导入和结构

    import org.scalatest.{Matchers, FlatSpec}
    import org.scalatest.prop.PropertyChecks
    
    class Fraction(n: Int, d: Int) {
    
      require(d != 0)
      require(d != Integer.MIN_VALUE)
      require(n != Integer.MIN_VALUE)
    
      val numer = if (d < 0) -1 * n else n
      val denom = d.abs
    
      override def toString = numer + " / " + denom
    }
    
    class PropertySpec extends FlatSpec with PropertyChecks with Matchers {
      forAll { (n: Int, d: Int) =>
    
        whenever(d != 0 && d != Integer.MIN_VALUE
          && n != Integer.MIN_VALUE) {
    
          val f = new Fraction(n, d)
    
          if (n < 0 && d < 0 || n > 0 && d > 0)
            f.numer should be > 0
          else if (n != 0)
            f.numer should be < 0
          else
            f.numer should be === 0
    
          f.denom should be > 0
        }
      }
    
      val invalidCombos =
        Table(
          ("n", "d"),
          (Integer.MIN_VALUE, Integer.MIN_VALUE),
          (1, Integer.MIN_VALUE),
          (Integer.MIN_VALUE, 1),
          (Integer.MIN_VALUE, 0),
          (1, 0)
        )
    
      forAll(invalidCombos) { (n: Int, d: Int) =>
        evaluating {
          new Fraction(n, d)
        } should produce[IllegalArgumentException]
      }
    }
    

    【讨论】:

    • 这是一个有效的SSCCE!非常感激。你可能为我省去了无数个小时的猜测,试图填补缺失的细节。 (您可能已经注意到,github 上的源代码省略了相同的细节。)
    • 很高兴为您提供帮助,他们没有解释的示例确实如此,有时很难找到导入,而且 github 也没有太大帮助,正如您所说,请如果您认为这是一个有效的答案,请标记它。提前致谢
    【解决方案2】:

    scalatest 的最新版本没有实现evaluating 并将=== 标记为过时。 因此,我将示例重写为:

    import org.scalatest._
    import org.scalatest.prop.PropertyChecks
    
    protected class Fraction(n: Int, d: Int) {
      require(d != 0 && d != Integer.MIN_VALUE && n != Integer.MIN_VALUE)
      val numer = if (d < 0) -1 * n else n
      val denom = d.abs
      override def toString = numer + " / " + denom
    }
    
    class SscceProps extends FlatSpec with PropertyChecks with Matchers {
      it should "valid combos" in { forAll { (n: Int, d: Int) =>
          whenever(d != 0 && d != Integer.MIN_VALUE && n != Integer.MIN_VALUE) {
            val f = new Fraction(n, d)
            if (n < 0 && d < 0 || n > 0 && d > 0) f.numer should be > 0
            else if (n != 0) f.numer should be < 0
            else f.numer shouldBe 0
            f.denom should be > 0
          }
      }}
      it should "invalid combos" in {
        forAll(Table(
          ("n", "d"),
          (Integer.MIN_VALUE, Integer.MIN_VALUE),
          (1, Integer.MIN_VALUE),
          (Integer.MIN_VALUE, 1),
          (Integer.MIN_VALUE, 0),
          (1, 0)
        )) { (n: Int, d: Int) =>
          an[IllegalArgumentException] should be thrownBy {new Fraction(n, d)}}
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 2015-11-21
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 2023-03-17
      • 2018-04-12
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多