Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:51:53+00:00 2026-06-13T22:51:53+00:00

There’s an old database test suite that I’m trying to migrate from Specs to

  • 0

There’s an old database test suite that I’m trying to migrate from Specs to Specs2. However, Specs2 runs the tests in a weird order (from my point of view), which breaks the tests since they change the database state, and run certain code twice.

Find below a simplified version of the tests. As far as I’ve understood, the tests should run in this order: (since I’ve specified sequential):
! 222, ! 333, ! 444
However, what actually happens, is that they’re executed in this order:
! 333, ! 222, ! 444

Here are the tests:

object IncludedSpec extends Specification {
  sequential
  println("sssstart")

  "IncludedSpec" should {

    println("..222")
    "context free block" >> {
      println("....! 222 the first test")
      1 === 1
    }

    var variableN = 0

    "block with context" >> {
      println("....333")
      "using one variable" >> {
        println("......! 333 doing some tests and setting variableN")
        variableN = 123
      }

      println("....444")
      "using it again" >> {
        println("......! 444 testing variableN")
        variableN === 123
      }
      println("....555")
    }
  }

  println("eeeend")
}

Here is all println output:

sssstart
eeeend
sssstart
eeeend
..222
....333
......! 333 doing some tests and setting variableN
....444
....555
....! 222 the first test
......! 444 testing variableN

And my two questions:

  1. Why isn’t ! 222 executed first?

  2. How is it possible that sssstart eeeend is output twice? The spec is an object and isn’t created twice?

Oddly enough, if I remove the side effects from the tests — that is, removing variableN and replacing the test bodies with ok — the tests run in the correct order.

Version details: I’m running these tests with Paly Framework 2.1-SNAPSHOT (version 203df0e from October 28 2012) and Scala 2.10.0-RC1. I think the Specs2 version bundled with Play is version 1.12, because the inline method is available, and it was added in 1.12(-SNAPSHOT), see https://github.com/etorreborre/specs2/issues/87 and there are no later Specs2 versions.

(Oh, and if you think I should completely rewrite the tests, then have a look at this question instead: How design a Specs2 database test, with interdependent tests? )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:51:54+00:00Added an answer on June 13, 2026 at 10:51 pm

    Originally, in specs2, you could create the following:

    1 – an example with in: "this is an example" in { 1 must_== 1 }

    2 – an example with >>: "this is an example" >> { 1 must_== 1 }

    3 – a block of examples with >>

    "this system should" >> {
      "do something" >> ok
      "do something else" >> ok
    }
    

    The important thing was that in was reserved for examples alone and accepted anything that could be converted to a Result. On the other hand >> could be used for both examples and groups of examples (to have a uniform style of nesting) so it accepted values of type Example or Result.

    Now things started to become a bit more complicated when you wanted to do the following:

    1 – use foreach to create a group of examples

    "this group has 5 examples" in {
      (1 to 5) foreach { i => "example "+i >> ok }
    }
    

    2 – use foreach to create a group of expectations

    "this example has 5 expectations" in {
      (1 to 5) foreach { i => i must_== i }
    }
    

    The trouble is, those 2 expressions with foreach both have type Unit. But they’re doing 2 very different things! The first one is building examples, so this expression needs to be evaluated right away to build the Specification. The second one is creating the body of an Example and will be executed later (or maybe never if the example is filtered out for example). Two things, with the same operator >>, that cannot work.

    So it was decided that >>(block: =>Unit) means “this builds a group of examples by a side-effect”, and in(expectations: =>Unit) means “this builds the body of an Example probably having expectations which will be a side-effect.

    Now, when this explains a bit better why you’re seeing a strange order with your print statements:

    ..222
    ....333
    ......! 333 doing some tests and setting variableN
    ....444
    ....555
    

    are printed first because they are contained in blocks of type Unit, interpreted as groups of Examples.

    And :

    ....! 222 the first test
    ......! 444 testing variableN
    

    are printed consequently because they are in blocks of type MatchResult[_], that is, they are considered as the body of examples.

    I agree that this is confusing and I hope that this explanation brings some perspective on why things are the way they are. Of course another lesson is “side-effects are sneaky because they don’t tell you what they are really doing”.

    So the general specs2 tip is to always end your blocks with proper values (unless you use a foreach construct as shown in my example above). For example, adding ok at the end of the blocks where you do a variable assignment should solve your issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is my code that I want to alert test once when mousemove ,
There is a moment in my app, that I need to force to show
There is a column that exists in 2 tables. In table 1, this column
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
There are a lot of blogs saying that a hasOwnProperty check should be used
There is a website called Gild.com that has different coding puzzles/challenges for users to
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
There are some stdlib functions that throw errors on invalid input. For example: Prelude>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.