【问题标题】:Using Scalacheck with Inside trait使用带有内部特征的 Scalacheck
【发布时间】:2018-05-31 02:51:06
【问题描述】:

我有一个非常简单的场景:测试任何一对长度为 10 的随机字符串作为参数传递到案例类 Pair(正在测试的自定义)应该是相同的。

class ExercisesPropSpec extends PropSpec with Checkers with Matchers with Inside{

  import Exercises._

  lazy val genPairs = for {
    left <- Gen.listOfN(10, Gen.alphaChar)
    right <- Gen.listOfN(10, Gen.alphaChar)
  } yield (left.mkString, right.mkString)

  property("pattern match tuples of alphanumerics to our custom pair class"){
    forAll(genPairs) {case (left: String, right: String) =>
      val pair = Pair(left, right)
      inside(pair) { case Pair(firstName, lastName) =>
        firstName shouldEqual(left)
        lastName shouldEqual(right)
      }
    }
  }
}

但是,当我从 sbt 运行 testOnly *ExercisesPropSpec 时,我得到了这个编译错误:

Compiling 1 Scala source to /home/vgorcinschi/ideaProjects/Learning Concurrent Programming in Scala/chapter01_introduction/target/scala-2.12/test-classes ...
[error] ~/ideaProjects/Learning Concurrent Programming in Scala/chapter01_introduction/src/test/scala/org/learningconcurrency/ExercisesPropSpec.scala:18:28: No implicit view available from org.scalatest.Assertion => org.scalacheck.Prop.
[error]     check(forAll(genPairs) {case (left: String, right: String) =>
[error]                            ^

A note 在 scalacheck-cookbook 中说

错误信息表明我们的属性检查不是 根据布尔逻辑进行评估

我希望最后的内部块应该返回布尔值。如果您能指出我在理解基于属性的测试或在此实现中使用 Inside trait 时遗漏了什么,我将不胜感激。

【问题讨论】:

    标签: scala scalatest scalacheck


    【解决方案1】:

    你只需要写

    firstName == left && lastName == right
    

    改为。

    (如果shouldEqual 确实返回了Boolean,则firstName shouldEqual(left) 的结果将被丢弃。)

    【讨论】:

    • 感谢您的关注 Alexey,我将 inside 块中的两行 should* 替换为 firstName == left &amp;&amp; lastName == right,不幸的是它产生了相同的错误
    • 对我上一条评论的小修正我把assert(firstName == left &amp;&amp; lastName == right)放在inside块内
    • 你不需要assert,按照你的第一条评论说的去做。
    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多