2012-09-17 49 views
1

specs2具有诸如Before,After,Around等特征,以便能够在安装/拆卸代码中包装示例。ScalaCheck之前/之后/周围?

有没有什么可以支持为ScalaCheck属性的每个“迭代”设置和拆除测试基础设施,即ScalaCheck要测试的每个值或一组值?

它看起来像specs2的各种各样的前,后,围绕特质设计围绕返回或抛出specs2结果实例,并不是一个结果。

+0

的问题是,虽然有从'Prop'到'Result'的隐式转换,没有人会以另一种方式回归。 –

回答

2

现在修复为最新的1.12.2-SNAPSHOT。你现在可以这样写:

import org.specs2.ScalaCheck 
import org.specs2.mutable.{Around, Specification} 
import org.specs2.execute.Result 

class TestSpec extends Specification with ScalaCheck { 
    "test" >> prop { i: Int => 
    around(i must be_>(1)) 
    } 

    val around = new Around { 
    def around[T <% Result](t: =>T) = { 
     ("testing a new Int").pp 
     try { t } 
     finally { "done".pp } 
    } 
    }  
} 

这将执行属性的“主体”之前和之后的代码。

您还可以更进一步,创造一个支持方法在隐around到你的道具经过:

class TestSpec extends Specification with ScalaCheck { 
    "test" >> propAround { i: Int => 
    i must be_>(1) 
    } 

    // use any implicit "Around" value in scope 
    def propAround[T, R](f: T => R) 
         (implicit a: Around, 
         arb: Arbitrary[T], shrink: Shrink[T], 
         res: R => Result): Result = 
    prop((t: T) => a(f(t))) 

    implicit val around = new Around { 
    def around[T <% Result](t: =>T) = { 
     ("testing a new Int").pp 
     try { t } 
     finally { "done".pp } 
    } 
    } 
} 
+0

谢谢!但是你提供的第一个代码示例不能用sonatype的最新快照编译。我尝试使用'sbt'从git编译specs2,但是得到了'[error] {file:/ home/robin/git/specs2/project /} plugins/*:update:sbt.ResolveException:unresolved dependency:com.jsuereth#xsbt -gpg-plugin; 0.6:找不到 [error]未解决的依赖关系:me.lessis#ls-sbt; 0.1.2:not found# –

+0

顺便说一句,我现在使用的是Scala 2.9.1。 –

+1

这种插件问题的确是由于Scala版本不匹配造成的。你可以摆脱构建文件中的插件。无论如何,我刚刚为Scala 2.9.1发布了1.12.2-SNAPSHOT,这样你就不需要编译自己了。 – Eric