假设我使用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 = 2
和timeout = 2.seconds
。如何定义一次并在规范中的所有await
次调用中重复使用它们?
答案 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