在指定的 should block of many in Specs2 中执行每个 before/after

Execute a before/after each in a specified should block out of many in Specs2

我写了如下规格:

class MySpec extends Specification {

  "A" should {
    "be true" in {
      true must beEqualTo(true)
    }
  }

  "B" should {
    "be false" in {
      false must beEqualTo(false)
    } 
  }
  ...
}

如何 I/Can 我指定 before/after 语句仅在 "B" 块(例如)内执行 each 测试。

您可以为您的测试创建上下文:

trait Context extends BeforeAfter {
  def before: Any = println("Doing setup")
  def after: Any = println("Done. Cleanup")
}

"B" should {
  "be false" in new Context  {
    false must beEqualTo(false)
   }
}

如果你需要用 specs2 做一些棘手的事情,你可以看看 this link