【发布时间】:2016-03-09 08:13:38
【问题描述】:
我有 2 个问题
我正在努力学习scalacheck
问题 1)
这是我正在编写的抛出错误的测试。您能否指出我应该阅读文档中的哪个页面以了解此错误背后的原因。
case class Student(name:String, age:Int, mathsScore:Int, scienceScore:Int){
require(name != null ,"Name cannot be blank")
require(age > 3 ,"Age should be more than 3")
require(mathsScore >= 0 , "Score should not be negative")
require(scienceScore >= 0 ,"Score should not be negative")
val totalScore = mathsScore + scienceScore
}
测试是
object CaseStudySpecification extends Properties("Case Study Specification") {
property("nullName") = forAll { (name: String, age: Int, ms: Int, ss: Int) =>
if (name == null)
Prop.throws(classOf[IllegalArgumentException]) {
val x = Student(name, age, ms, ss)
}
}
}
错误是
No implicit view available from AnyVal => org.scalacheck.Prop.
[error] property("nullName") = forAll { (name: String, age: Int, ms: Int, ss: Int) =>
[error] ^
问题 2)
官方文档给出了一个示例测试类
property("stringLength") = Prop.forAll { s: String =>
val len = s.length
(s+s).length == len+len
}
我还读到它可以写成
val stringLength = Prop.forAll { s: String =>
val len = s.length
(s+s).length == len+len
}
我如何运行第二种形式的测试代码,因为当我运行 sbt test 时,第二个版本没有任何反应。
以上两个sn-ps都在
object Ch3 extends Properties("String") {
}
【问题讨论】:
-
我将原文的后半部分拆分为一个单独的 StackOverflow 问题:stackoverflow.com/questions/38284055/…
标签: scalacheck