The Artima Developer Community
Sponsored Link

ScalaTest/ScalaUtils Forum
Nested before and after for FunSpec

1 reply on 1 page. Most recent reply: Oct 7, 2012 9:15 AM by Bill Venners

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Eugene Prystupa

Posts: 1
Nickname: 79741
Registered: Nov, 2011

Nested before and after for FunSpec Posted: Oct 6, 2012 5:58 PM
Reply to this message Reply
Advertisement
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);
    });
  });
});


Thanks,
Eugene


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Nested before and after for FunSpec Posted: Oct 7, 2012 9:15 AM
Reply to this message Reply
> 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:

http://doc.scalatest.org/1.8/index.html#org.scalatest.path.FunSpec

> 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
  }
}

Flat View: This topic has 1 reply on 1 page
Topic: ShouldMatchers not work for objects with length and size Previous Topic   Next Topic Topic: scalatest + jenkins + sbt

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use