如何在包装函数中执行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
答案 0 :(得分:7)
您可以使用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
上下文(及其Example
对应项)的某些组合可能更合适。