为什么不评估Scalacheck Prop值? [英] Why does a Scalacheck Prop value not get evaluated?

查看:109
本文介绍了为什么不评估Scalacheck Prop值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scalacheck官方文档提供了以下示例:

The official scalacheck documentation gives the following example:

  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? When I execute sbt test, nothing happens with the second version.

推荐答案

问题是第二个版本只是声明一个val,该val包含对所讨论属性的引用,但不足以让scalacheck对其进行评估.这种声明属性的样式很有用,例如,如果您想在其他基本属性之外组成一个新属性.您可以直接对其进行检查,也可以为其分配特殊的属性设置器,以使其在测试过程中得到评估:

The problem is that the second version is simply declaring a val that holds a reference to the property in question but that isn't enough to get scalacheck to evaluate it. This style of declaring a property is useful for example if you want to compose a new property out of other basic properties. You can check it directly or to assign it the special property setter in order to get it to be evaluated as part of the test run:

val stringLength = Prop.forAll { s: String =>
  val len = s.length
  (s+s).length == len+len
}

// invoke directly:
stringLength.check

// alternatively, just declare it the usual way
property("stringLength") = stringLength

这本身并不是很有用,它的使用方式可能是这样的:

This isn't very useful by itself, the way it is meant to be used is perhaps something like this:

property("composite") = Prop.all(stringLength, prop2, prop3, ...)

这篇关于为什么不评估Scalacheck Prop值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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