BeforeAfter and Examples

24 views
Skip to first unread message

Edmondo Porcu

unread,
Apr 30, 2013, 12:02:47 PM4/30/13
to specs2...@googlegroups.com

Dear all,

I was trying to set up a Scope for my examples which needed some setup and cleaning procedure.

 

I noticed that, however, if I use a trait which extends BeforeAfter and I do the following:

 

Trait MyContextWithSetupAndCleanup extends BeforeAfter

 

"When something " in new  MyContextWithSetupAndCleanup ("testSpace3", "testGroup3") {

      result must beSuccessful

 

 

    }

 

The code in the MyContextWithSetupAndCleanup Before method is executed twice:

 

1 When the example is created

1 when the example is executed

 

I am using a mutable specification with sequential.

 

 

Am I making wrong usage of specs2 features?

 

Best

 

 

 

 

 

Edmondo Porcu

 

 


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

etorreborre

unread,
Apr 30, 2013, 4:48:23 PM4/30/13
to specs2...@googlegroups.com
Hi Edmondo,

I can't reproduce your problem with this simple spec:

import org.specs2._

class TestSpec extends mutable.Specification{

  trait test extends mutable.BeforeAfter {
    def before = "before".pp
    def after = "after".pp
  }

  "test" >> new test {
    ok.pp
  }
}

Can you please check that you are using the "mutable.BeforeAfter" trait? And if this doesn't work can you post a short but complete snippet showing the problem?

Thanks,

Eric.

Edmondo Porcu

unread,
May 1, 2013, 4:44:28 AM5/1/13
to specs2...@googlegroups.com

Hello Eric,

 

I found out that if instead of a trait you use a class and you put something in the constructor, things don’t work as you expect.

 

Without code inside the constructor:

 

@RunWith(classOf[JUnitRunner])

class Spec2ProblemWithBeforeAfter extends Specification{

 

  abstract class test extends BeforeAfter {

    def before = "before".pp

    def after = "after".pp

  }

 

  sequential

  "The BeforeAfter before and before" should {

    "Not be called twice" in new test{

      "a" must_== "a"

 

    }

  }

 

}

 

 

Testing started at 10:39 ...

before

afterThe BeforeAfter before and before should

Not be called twice

 

Process finished with exit code 0

 

With code inside the constructor:

 

@RunWith(classOf[JUnitRunner])

class Spec2ProblemWithBeforeAfter extends Specification{

 

  abstract class test extends BeforeAfter {

    println("hello")

    def before = "before".pp

    def after = "after".pp

  }

 

  sequential

  "The BeforeAfter before and before" should {

    "Not be called twice" in new test{

      "a" must_== "a"

 

    }

  }

 

}

 

before

hello

after

before

after

 

Process finished with exit code 0

 

I have the feeling that using scala.DelayedInit inside BeforeAfter might be tricky…

 

Best regards

 

Edmondo

--
You received this message because you are subscribed to the Google Groups "specs2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specs2-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


 
 

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

etorreborre

unread,
May 1, 2013, 6:54:55 AM5/1/13
to specs2...@googlegroups.com
I see, 

This is indeed a problem with DelayedInit but this is also the only way to make the BeforeAfter trait work with mutable specifications.

I don't think that there's any work-around other than moving the code that's inside the body of your class to a proper method. This is also a best practice :-)

E.

Edmondo Porcu

unread,
May 1, 2013, 8:29:00 AM5/1/13
to specs2...@googlegroups.com

In my case it was only lazy vals.

 

What do you suggest?

 

Best Regards

etorreborre

unread,
May 1, 2013, 9:04:57 AM5/1/13
to specs2...@googlegroups.com
Are you sure that it is only lazy vals?

When I try this:

  abstract class test extends mutable.BeforeAfter {
    println("hello")

    def before = "before".pp
    def after = "after".pp
  }

I get the issue you mention, but if I modify it to this:

  abstract class test extends mutable.BeforeAfter {
    lazy val message = println("hello")

    def before = "before".pp
    def after = "after".pp
  }


Then it works fine.

E.
Reply all
Reply to author
Forward
0 new messages