2013-09-14 6 views
4

Я пытаюсь запустить mutable specs2 с помощью методов After и Around. У меня есть следующие:Использование mutable Before/After/Around in 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 

} 

Когда я выполнить тест, только метод вокруг выполняется. Что я делаю не так?

ответ

4

Я подозреваю, что метод Around trait's delayedInit переопределяет тот же метод в After.

Обратите внимание, что вы можете просто вызвать свою логику после AsResult.effectively(t) для получения требуемого эффекта.

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

Да, но я пытаюсь разделить проблемы, поэтому мне нужно, чтобы они работали отдельно. – netta

Смежные вопросы