在Specs2中使用可变的Before / After / Around

时间:2013-09-14 10:42:44

标签: scala specs2

我正在尝试使用After和Around方法运行可变的specs2测试。 我有以下内容:

import org.specs2.mutable.{Specification, Around, After}
import org.specs2.specification.Scope
import org.specs2.execute.{Result, AsResult}

trait Foo extends After with Around {

  override def apply[T: AsResult](a: => T): Result = {
    lazy val result = super[Around].apply(a)
    super[After].apply(result)
  }

  override def after: Any = {
    println("after-method\n")
  }

  override def around[T: AsResult](t: => T) = {
    try {
      println("around-method\n")
      AsResult.effectively(t)
    } catch {
      case e: Throwable => {
        //preform some logic here
        throw e
      }
    }
  }
}

class Specs2Test extends Specification {
  "This test" should {
    "run with around and after" in new Context {
      true must_== true
    }
  }

  trait Context extends Scope with Foo

}

执行测试时,只执行around方法。我做错了什么?

1 个答案:

答案 0 :(得分:4)

我怀疑Around特征的delayedInit方法覆盖了After中的相同方法。

注意,你可以在AsResult.effectively(t)之后调用你的后逻辑来获得所需的效果。

def around[T : AsResult](t: =>T) {
  // before logic
  val result = AsResult.effectively(t)
  // after logic
  result
}