Search code examples
scalaunit-testingspecs2

Specs2, sequential or not


I'm starting a new project with Scala. I would like to test my components with Specs2. But I have just lost one half hour to solve problems with verifications on a mock.

My specification is below:

Creating a page should 
  Verify that the name is not empty
  Verify that the name is unique
  Produce and store the page

I have mock for my repository so that I can verify that when a precondition fail, the page is not stored:

there was no(pages).add(any[Page]) 

However for the last sentence, I verify that the page is stored.

there was one(pages).add(page)

But my specifications was failing because the last test was made before the second one and thus there was one(pages).add(...) so I have to add the sequential keyword.

Am I doing something wrong? May I create a mock for each example so that there are no side effects. Or may I reset the mock before each example?

Or is it just normal to use sequential when testing with mocks?

Thanks a lot.


Solution

  • Since you are using a mock the easiest thing to do is to create one per example. Like this all the examples are independent and can be executed concurrently.