Tomer
Tomer

Reputation: 2448

specs2 - how to use the same variable inside around and inside the test itself?

I am using specs2 as my test framework. I want to generate a uniq key that will be available in the test itself.

def around[R: AsResult](r: => R): Result = {
   val uniqueToken = before()
   try AsResult(r)(uniqueToken)
   finally after(uniqueToken)
}

"foo" should {
   "bar" in {
     do something with uniqueToken
   }
}

Couldn't find any good way to do it.. Any idea?

Upvotes: 0

Views: 94

Answers (2)

Eric
Eric

Reputation: 15557

You can write this

class MySpec extends Specification with ForEach[Token] {
  "foo" should {
     "do something" in { token: Token =>
        ok
     }
  }

  def foreach[R : AsResult](f: Token => R): Result = {
    val token = createToken

    try AsResult(f(token))
    finally cleanup(token)
  }
}

This is documented here.

Upvotes: 1

Rado Buransky
Rado Buransky

Reputation: 3294

You should get the general idea from this pseudocode:

class Around[R: AsResult](r: => R) {
   val uniqueToken = before()

   try AsResult(r)(uniqueToken)
   finally after(uniqueToken)
}

"foo" should {
   "bar" in new Around(r) {
     do something with uniqueToken
   }
}

Upvotes: 0

Related Questions