These are chat archives for etorreborre/specs2

25th
Dec 2018
David Vaks
@davidvaks
Dec 25 2018 14:58

@etorreborre Hi,

We have a case, where we need a couple of fragments to depend on the result of an insertion to an external service,
I've tried to describe the use case with this code:

    case class ExpectedResult(header: String, matcher: Matcher[ExternalServiceResult])

    "this is a block of examples" in {
        // set-up something
        val id: String = externalService.createNewThing("something")

        val expectedResults = 
              Seq(ExpectedResult(authorizationHeader1, throwAn[Exception]), 
                       ExpectedResult(authorizationHeader2, ===(ExternalServiceResult("something"))))

        Fragments.foreach(expectedResults) { expectedResult =>
              s"for ${expectedResult.header} header" in {
                        externalService.getSomething(expectedResult.header, id) must expectedResult.matcher
               }
          }
    }

When running the tests we get this message:

 org.specs2.specification.dsl.mutable.SpecificationCreationException: 
...
This means that you have some code which should be enclosed in an example. Instead of writing:

 "this is a block of examples" in {
   // set-up something
   createDatabase
   "first example" in { 1 must_== 1 }
   "second example" in { 1 must_== 1 }
 }

You should write:

 "this is a block of examples" in {
   "the setup must be ok" in {
     createDatabase must not(throwAn[Exception])
   }
   "first example" in { 1 must_== 1 }
   "second example" in { 1 must_== 1 }
 }

But, if we put the externalService.createNewThing("something") call inside an example like suggested, we'll be unable to get the id (the result) and use it in the next tests.
Is there a way to share this 'state' between the fragments?
Thanks in advance!

Nikolaus Rumm
@woyzeck
Dec 25 2018 16:14
Hi, what’s the recommended way to organize and reuse single workflow steps when writing integration tests (Play) using specs2 and the WithBrowser trait ? What I’d like to achieve is reuse basic interactions like “user signs in” and “user opens his options page”.
Nikolaus Rumm
@woyzeck
Dec 25 2018 16:44
This seems to be a trivial question and I tried to solve it by mixing in a trait that implements those workflow steps, but I’m lost in dependencies ;)