4

我正在尝试使用 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 方法。我究竟做错了什么?

4

1 回答 1

4

我怀疑Aroundtrait 的delayedInit方法覆盖了After.

请注意,您可以简单地调用 after 逻辑AsResult.effectively(t)以获得所需的效果。

def around[T : AsResult](t: =>T) {
  // before logic
  val result = AsResult.effectively(t)
  // after logic
  result
}
于 2013-09-14T10:55:43.757 回答