scala - 在 specs2 框架中,为什么使用 Scope 会阻止执行 forAll 量词?

标签 scala specs2 scalacheck

在下面的代码中,如何让 Specs2 执行第一个测试? “print ones”测试在它应该失败的时候通过了。由于 new ScopeforAll() 部分中的代码未执行。

println 语句仅用于跟踪输出。如果您看到任何以“one”开头的行,请告诉我。

空的 Scope 只是为了演示问题。这是从我实际在 Scope 中使用变量的代码中删除的。

import org.scalacheck.Gen
import org.scalacheck.Prop._
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

class TestingSpec extends Specification with ScalaCheck {
  "sample test" should {
    "print ones" in new Scope { 
      println("The first test passes, but should fail")
      forAll(Gen.posNum[Int]) { (x: Int) =>
          println("one: " + x)
          (0 < x) mustNotEqual true
      } 
    } 

    "print twos" in { 
      println("The second test passes and prints twos")
      forAll(Gen.posNum[Int]) { (x: Int) =>
        println("two: " + x)
        (0 < x) mustEqual true
      } 
    } 
  }
}

这是我的输出:

sbt> testOnly TestingSpec
The second test passes and prints twos
The first test passes, but should fail
two: 1
two: 2
two: 1
    ...
two: 50
two: 34
two: 41
[info] TestingSpec
[info] 
[info] sample test should
[info]   + print ones
[info]   + print twos
[info] 
[info] Total for specification TestingSpec
[info] Finished in 96 ms
[info] 2 examples, 101 expectations, 0 failure, 0 error
[info] 
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed Apr 28, 2015 3:14:15 PM

附言我将我的项目依赖项从版本 2.4.15 更新到 specs2 3.5。还是有这个问题...

最佳答案

这是因为您放入 Scope 中的任何内容在出现故障时都必须抛出异常。 ScalaCheck Prop 除非您执行它,否则不会执行任何操作,因此您的示例将始终通过。

解决方法是将 Prop 转换为 specs2 Result,例如使用以下隐式:

import org.specs2.execute._

implicit class RunProp(p: Prop) {
  def run: Result = 
    AsResult(p)
}

然后

"print ones" in new Scope { 
  println("The first test passes, but should fail")
   forAll(Gen.posNum[Int]) { (x: Int) =>
     println("one: " + x)
      (0 < x) mustNotEqual true
   }.run 
} 

关于scala - 在 specs2 框架中,为什么使用 Scope 会阻止执行 forAll 量词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930670/

相关文章:

scala - 如何在Scala中将枚举转换为Seq/List?

scala - Specs2 - 如何吞掉异常

sbt - specs2:标记规范/单独测试并使用 sbt 将其排除在运行之外

scala - 在没有匹配器的情况下如何跳过specs2中的测试?

scala - 无法将 ScalaCheck 与 Scala IDE for Eclipse 结合使用

scala - 抽象父类(super class) scala 中的断言创建 NPE

scala - 使用 -Ywarn-unused 时从 Scala 中的分析中排除类型证据参数

design-patterns - 双折功能图案

scala - scala 中的类型不匹配错误以供理解

scala - 在 ScalaCheck 中从语法生成字符串