Question

I am trying to run a mutable specs2 test with After and Around methods. I have the following:

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

}

When I execute the test, only the around method is executed. What am I doing wrong?

Was it helpful?

Solution

I suspect that the Around trait's delayedInit method is overriding the same method in After.

Note, you could simply call your after logic after AsResult.effectively(t) for the required effect.

def around[T : AsResult](t: =>T) {
  // before logic
  val result = AsResult.effectively(t)
  // after logic
  result
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top