如何在包装函数中执行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") } } }
所以,这三个测试中的每一个都应该在内部执行
with(someSession){…
使用ScalaTest时,我可以用theFixture覆盖它
解决方法
你可以使用像
AroundExample
这样的东西:
class HelloWorldSpec extends Specification with AroundExample { def around[T <% Result](t: =>T) = inWhateverSession(t) ... }
class HelloWorldSpec extends Specification { implicit object sessionContext = new Around { def around[T <% Result](t: =>T) = inWhateverSession(t) } ... }
根据您需要做什么,Before,BeforeAfter或Outside上下文(及其示例对应项)的某些组合可能更适合.