在Scala Specs2中重用“await”参数

时间:2014-01-02 12:21:28

标签: scala future specs2 matcher

假设我使用Futures测试specs2(见下文)的结果。

def f1():Future[String] = {...}
def f2():Future[String] = {...}

我有Matchers[String]来检查结果

def m1():Matcher[String] = {...}
def m2():Matcher[String] = {...}

现在,我可以使用implicits创建Matcher[Future[String]]

def fm1():Matcher[Future[String]] = m1.await(retries=2, timeout=2.seconds)
def fm2():Matcher[Future[String]] = m2.await(retries=2, timeout=2.seconds)   

到目前为止一直很好,但我不喜欢重复 retries = 2timeout = 2.seconds。如何定义一次并在规范中的所有await次调用中重复使用它们?

1 个答案:

答案 0 :(得分:3)

case class Retry(value:Int) extends AnyVal
case class Timeout(value:Duration) extends Anyval

implicit class MatcherWithImplicitValues[A](m:Matcher[A]) {
  def awaitWithImplicitValues(implicit r:Retry, t:Timeout) = {
    m.await(retries = r.value, timeout = t.value)
  }
}

implicit val retries = Retry(2)
implicit val timeout = Timeout(2 seconds)

m1.awaitWithImplicitValues