将 Scalacheck 与 Inside trait 结合使用
Using Scalacheck with Inside trait
我有一个非常简单的场景:测试作为参数传递到案例中的任何一对长度为 10 的随机字符串 class 对(正在测试的自定义字符串)应该相同。
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 中说
what the error message indicates is that our property check is not
evaluating in terms of Boolean logic
我希望最后的内部块应该返回布尔值。如果您能指出我在理解基于 属性 的测试或在此实现中使用 Inside trait 时遗漏了什么,我将不胜感激。
你只需要写
firstName == left && lastName == right
相反。
(如果 shouldEqual
做了 return Boolean
,firstName shouldEqual(left)
的结果将被丢弃。)
我有一个非常简单的场景:测试作为参数传递到案例中的任何一对长度为 10 的随机字符串 class 对(正在测试的自定义字符串)应该相同。
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 中说
what the error message indicates is that our property check is not evaluating in terms of Boolean logic
我希望最后的内部块应该返回布尔值。如果您能指出我在理解基于 属性 的测试或在此实现中使用 Inside trait 时遗漏了什么,我将不胜感激。
你只需要写
firstName == left && lastName == right
相反。
(如果 shouldEqual
做了 return Boolean
,firstName shouldEqual(left)
的结果将被丢弃。)