如何在包装函数中执行 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时,我可以用Fixture来覆盖。
你可以
使用类似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
对应项)的某种组合可能更适合。