Scala 在Specs2中使用可变的前后/前后

Scala 在Specs2中使用可变的前后/前后,scala,specs2,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: AsR

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

我怀疑trait的
delayedInit
方法在
之后的
中覆盖了相同的方法

注意,您只需在
AsResult.effective(t)
之后调用after逻辑即可获得所需的效果

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

是的,但我正在尝试分离关注点,所以我需要它们单独工作。