如何将Specs2与Scalacheck结合使用来自动测试字符串参数?

如何将Specs2与Scalacheck结合使用来自动测试字符串参数?,scala,specs,scalacheck,specs2,Scala,Specs,Scalacheck,Specs2,Scala重写的测试框架集成了自动化测试和。specs2文档中给出的示例介绍了如何将scalacheck与specs2或更复杂的自定义生成器一起使用,如中所示 在尝试让一个稍微不那么复杂的示例正常工作时,我遇到了困难,因为如果我想生成字符串参数而不是整数,我不知道如何使用specs2和scalacheck。这个快速入门示例将如何实现 导入org.scalacheck_ 对象字符串规范扩展属性(“字符串”){ 属性(“startsWith”)=Prop.forAll((a:String,b:St

Scala重写的测试框架集成了自动化测试和。specs2文档中给出的示例介绍了如何将scalacheck与specs2或更复杂的自定义生成器一起使用,如中所示

在尝试让一个稍微不那么复杂的示例正常工作时,我遇到了困难,因为如果我想生成字符串参数而不是整数,我不知道如何使用specs2和scalacheck。这个快速入门示例将如何实现


导入org.scalacheck_

对象字符串规范扩展属性(“字符串”){ 属性(“startsWith”)=Prop.forAll((a:String,b:String) =>(a+b)。从(a)开始

属性(“endsWith”)=Prop.forAll((a:String,b:String) =>(a+b)。结束(b))

//这真的总是真的吗? 属性(“concat”)=Prop.forAll((a:String,b:String)=> (a+b).长度>a.长度和(a+b).长度>b.长度 )

属性(“子字符串”)=Prop.forAll((a:String,b:String)=> (a+b).子字符串(a.长度)==b )

属性(“子字符串”)=Prop.forAll((a:String,b:String,c:String)=> (a+b+c).子字符串(a.length,a.length+b.length)==b ) }


从外观上看,如果它是使用scalacheck集成作为Specs2规范编写的?

一个非常直接的翻译是使用
check
方法和简单的函数:

package test

import org.specs2._
import org.scalacheck._

class ScalaCheckExamples extends Specification with ScalaCheck { def is =

  "startsWith" ! check { (a: String, b: String) => (a+b).startsWith(a) }                                                ^
  "endsWith"   ! check { (a: String, b: String) => (a+b).endsWith(b) }                                                  ^
  "concat"     ! check { (a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length }                 ^
  "substring"  ! check { (a: String, b: String) => (a+b).substring(a.length) == b }                                     ^
  "substring"  ! check { (a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b }     ^
                                                                                                                        end
 }
而输出实际上表明
concat
属性不正确:

  [info] + startsWith
  [info] + endsWith
  [error] x concat
  [error]   A counter-example is ['', ''] (after 0 try)
  [error] the value is false
  [error]  (ScalaCheckExamplesSpec.scala:6)
  [info] + substring
  [info] + substring
  [info]
  [info] Total for specification ScalaCheckExamplesSpec
  [info] Finished in 7 seconds, 547 ms
  [info] 5 examples, 401 expectations, 1 failure, 0 error
  [info]

Eric。

有关在specs2中使用ScalaCheck库的更多信息,请查看。

感谢您的回答。这确实为我澄清了一些事情。也许你想考虑将这个例子添加到示例代码树中?我为上面的站点得到了一个HTTP 404。
  [info] + startsWith
  [info] + endsWith
  [error] x concat
  [error]   A counter-example is ['', ''] (after 0 try)
  [error] the value is false
  [error]  (ScalaCheckExamplesSpec.scala:6)
  [info] + substring
  [info] + substring
  [info]
  [info] Total for specification ScalaCheckExamplesSpec
  [info] Finished in 7 seconds, 547 ms
  [info] 5 examples, 401 expectations, 1 failure, 0 error
  [info]