没有可用的AnyVal =>隐式视图. org.scalacheck.P​​rop. [错误]属性 [英] No implicit view available from AnyVal => org.scalacheck.Prop. [error] property

查看:99
本文介绍了没有可用的AnyVal =>隐式视图. org.scalacheck.P​​rop. [错误]属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题

我正在尝试学习scalacheck

I am trying to learn scalacheck

问题1)

这是我正在编写的引发错误的测试.您能指出我应该阅读文档中的哪一页才能了解此错误的原因.

Here is the test I am writing which is throwing the error. Can you please point to which page from docmentation i should read to understand reason behind this error.

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)

官方文档给出了一个示例测试类,

The official documentation gives one example test class as

  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时,第二种版本什么都没有发生.

How can i run the second form of test code , as when i run sbt test nothing happens for second version.

以上两个片段都在

object Ch3 extends Properties("String") {

}

推荐答案

The signature of Prop.forAll being called requires a function returning Prop (or at least something that can be implicitly converted to Prop) but, as written, the function:

(name: String, age: Int, ms: Int, ss: Int) => {
  if (name != null) Prop.throws(...)
}

具有(String,Int,Int,Int)=> AnyVal的推断签名,并且不存在隐式转换为属性的功能.因此,编译错误.通过确保始终返回如下所示的布尔值,可以简单地修复该函数:

has an inferred signature of (String, Int, Int, Int) => AnyVal, and there does not exist an implicit conversion into a property. Hence the compilation error. The function could be trivially fixed by making sure it always returns a Boolean as follows:

property("nullName") = forAll { (name: String, age: Int, ms: Int, ss: Int) =>
  if (name == null) Prop.throws(classOf[IllegalArgumentException]) {
    Student(name, age, ms, ss)
  } 
  else true
}

这会导致应用隐式Boolean => Prop 函数,然后编译代码.更为惯用的解决方法是使用隐含运算符重写属性:

This results in the implicit Boolean => Prop function being applied, and the code compiles. A more idiomatic fix would be to rewrite the property using the implication operator:

property("nullName") = forAll { (name: String, age: Int, ms: Int, ss: Int) =>
  name == null ==>
    Prop.throws(classOf[IllegalArgumentException]) {
      Student(name, age, ms, ss)
    }
}

但是,拒绝太多生成的输入不是一个好主意,并且由于scalacheck生成的第一个值实际上为空,因此该属性最终显示为不确定",因此测试仍然失败.您可以将属性简化为:

However it is not a good idea to reject too much of the generated input and since the very first value that scalacheck generates is in fact null, the property ends up as 'undecided' so the test still fails. You could just simplify your property to:

property("nullName") = forAll { (age: Int, ms: Int, ss: Int) =>
  Prop.throws(classOf[IllegalArgumentException]) {
    Student(null, age, ms, ss)
  }
}

由于这不是特定于scalacheck的问题,而是一个通用的Scala问题,因此scalacheck文档中没有专门讨论该问题;您可以阅读隐式视图以获得更多背景知识.

As this isn't a scalacheck-specific problem but rather a general Scala one it isn't specifically covered in the scalacheck documentation; you can read up on implicit views for more background.

这篇关于没有可用的AnyVal =>隐式视图. org.scalacheck.P​​rop. [错误]属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆