执行包含在函数中的specs2示例

时间:2012-04-18 19:28:49

标签: unit-testing scala specs2

如何在包装函数中执行spec2规范的所有测试?

例如:

class HelloWorldSpec extends Specification {

    wrapAll(example) = {
        // wrap it in a session, for example.
        with(someSession){
            example()
        }
    }

    "The 'Hello world' string" should {
      "contain 11 characters" in {
        "Hello world" must have size(11)
      }
      "start with 'Hello'" in {
        "Hello world" must startWith("Hello")
      }
      "end with 'world'" in {
        "Hello world" must endWith("world")
      }
    }
  }

因此,这3个测试中的每一个都应该在

中执行
  

与(someSession){...

使用ScalaTest时,我可以覆盖 withFixture

1 个答案:

答案 0 :(得分:7)

您可以使用AroundExample

之类的内容
class HelloWorldSpec extends Specification with AroundExample {
  def around[T <% Result](t: =>T) = inWhateverSession(t)
  ...
}

implicit context object

class HelloWorldSpec extends Specification {
  implicit object sessionContext = new Around {
    def around[T <% Result](t: =>T) = inWhateverSession(t)
  }
  ...
}

根据您确切需要做什么,BeforeBeforeAfterOutside上下文(及其Example对应项)的某些组合可能更合适。