I'm trying to write a mutable Spec that has a value passed in by a Scope. I also need to be able to do some cleanup after each test runs. Following the documentation, I tried using an Outside combined with an After but got mixed results.
Is the fifth example the correct approach for this or am I missing something basic?
import org.specs2.mutable.{After, Specification}
import org.specs2.specification.Outside
class ExampleSpec extends Specification {
"Tests using Outside and After" >> {
"#1 doesn't run the after" in c1() {
(m: String) => {
println("Running test 1.")
success
}
}
"#2 doesn't actually run the test" in new c2() {
(m: String) => {
println("Running test 2.")
failure
}
}
"#3 doesn't run the after" in (new t3{}) {
(m: String) => {
println("Running test 3.")
success
}
}
"#4 doesn't actually run the test" in new t4 {
(m: String) => {
println("Running test 4.")
failure
}
}
"#5 works, but is it the right way?" in new t5 {
val sessionKey = outside // The test would have to call outside?
println("Running test 5.")
success
}
}
trait common extends Outside[String] with After {
def name: String
def outside = "Used by the real test."
def after = println("After for " + name)
}
case class c1() extends common { def name = "c1" }
case class c2() extends common { def name = "c2" }
trait t3 extends common { def name = "t3" }
trait t4 extends common { def name = "t4" }
trait t5 extends common { def name = "t5" }
}
This gives the following output:
Running test 1.
After for c2
Running test 3.
After for t4
Running test 5.
After for t5
[info] ExampleSpec
[info]
[info] Tests using Outside and After
[info] + #1 doesn't run the after
[info] + #2 doesn't actually run the test
[info] + #3 doesn't run the after
[info] + #4 doesn't actually run the test
[info] + #5 works, but is it the right way?
[info]
[info] Total for specification ExampleSpec
[info] Finished in 19 ms
[info] 5 examples, 0 failure, 0 error
Note: Per Eric's comments to Question 21154941, I realize tests 1 and 2 are not the correct approach. I put them here to be exhaustive and because at other times, you can use case classes for contexts/variable isolation.
In your case the simplest thing to do is to use the org.specs2.specification.FixtureExample
trait:
class ExampleSpec extends Specification with FixtureExample[F] {
"Tests using a fixture" >> {
"you can use the fixture values" in { f: F =>
println("Running test 1 with "+f.sessionKey)
success
}
"there is cleanup after each example" in { f: F =>
success
}
}
def fixture[R : AsResult](f: F => R): Result = {
try AsResult(f(F()))
finally cleanup
}
def cleanup = println("cleanup")
}
case class F(sessionKey: String = "key")