Any reason why before and after not allowed on multiple level within describe, just like beforeEach and afterEach are supported in Jasmine: http://pivotal.github.com/jasmine/ (search for "nesting describe blocks")?
Nested before and each are an excellent way to build up the state for all different [nested] use cases and I don't see any way to replicate this in Scalatest.
Below is an example of nested test script written in Jasmine that I would love to replicate in Scalatest, but can't:
describe("A spec", function() {
var foo;
beforeEach(function() {
foo = 0;
foo += 1;
});
afterEach(function() {
foo = 0;
});
it("is just a function, so it can contain any code", function() {
expect(foo).toEqual(1);
});
it("can have more than one expectation", function() {
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
describe("nested inside a second describe", function() {
var bar;
beforeEach(function() {
bar = 1;
});
it("can reference both scopes as needed ", function() {
expect(foo).toEqual(bar);
});
});
});
> Any reason why before and after not allowed on multiple > level within describe, just like beforeEach and afterEach > are supported in Jasmine: > http://pivotal.github.com/jasmine/ (search for "nesting > describe blocks")? > > Nested before and each are an excellent way to build up > the state for all different [nested] use cases and I don't > see any way to replicate this in Scalatest. > The way to do what you want in ScalaTest is to use a "path" trait (from the org.scalatest.path package). In a path trait, if you place code lexically before a test, it will happen before. If you place it after the test, it will happen after, because for each test, ScalaTest will execute just the "path" to and from that test. Here's the documentation:
> Below is an example of nested test script written in > Jasmine that I would love to replicate in Scalatest, but > can't: > The way you might translate that class to ScalaTest is:
import org.scalatest.path
import org.scalatest.matchers.ShouldMatchers
import scala.collection.mutable.ListBuffer
class ExampleSpec extends path.FunSpec with ShouldMatchers {
describe("a spec") {
var foo = 0 // Will happen before each test, because
foo += 1 // located lexically before each test
it("is just a function, so can contain any code") {
foo should equal (1)
}
if("can have more than one assertion") {
foo should equal (1)
true should be (true)
}
describe("nested inside a second describe") {
var bar = 0 // Will happen before just the next test
bar = 1 // because lexically before only that test
it("can reference both scopes as needed") {
bar should equal (foo)
}
}
foo = 0 // Will happen after each test because
// locally lexically after each test
}
}