我正在尝试使用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方法。我做错了什么?
答案 0 :(得分:4)
我怀疑Around
特征的delayedInit
方法覆盖了After
中的相同方法。
注意,你可以在AsResult.effectively(t)
之后调用你的后逻辑来获得所需的效果。
def around[T : AsResult](t: =>T) {
// before logic
val result = AsResult.effectively(t)
// after logic
result
}