If you use Given / When / Then steps, you can use Context to control what happens before and after each example.
import org.specs2._ import specification._ class MySpec extends Specification { def is = "anonymous" ^ "something" ^ something ^ "must happen" ^ mustHappen ^ endp^ "OAuth"^ "signing" ^ signing ^ "works ok" ^ worksOk lazy val something: Given[Int] = (s: String) => { s.pp; 1 } lazy val mustHappen: Then[Int] = (i: Int) => (s: String) => context { s.pp; i must_== 1 } lazy val signing: Given[Int] = (s: String) => { s.pp; 2 } lazy val worksOk: Then[Int] = (i: Int) => (s: String) => context { s.pp; i must_== 2 } lazy val context = new BeforeAfter { def before = "before".pp def after = "after".pp } }
In the above code, the apply method of the context object is used to port the code executed with before and after . Also, if you add an error to one of the examples, you will see that the "after" code is always executed.
PS: pp is a utility method, for example println to display something in the terminal at runtime. The advantage over println is that it returns its argument so you can write 1.pp must_== 1
source share