【问题标题】:Using mutable Before/After/Around in Specs2在 Specs2 中使用可变的 Before/After/Around
【发布时间】:2013-09-14 19:10:47
【问题描述】:

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

【问题讨论】:

    标签: scala specs2


    【解决方案1】:

    我怀疑Around trait 的delayedInit 方法覆盖了After 中的相同方法。

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

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

    【讨论】:

    • 是的,但我正在尝试分离关注点,因此我需要它们单独工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2013-09-25
    • 2012-03-15
    相关资源
    最近更新 更多