user3519173
user3519173

Reputation: 75

specs and specs2: how to implement doBefore{} in specs2?

I'm having hard time with "transfering" things in my scala test classes from specs to specs2. Last thing I have is issue with doBefore{} and some "test" in {}

My "testing" should { doBefore{} and some "getting" in {} } give me this error

Description Resource Path Location Type

could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Unit]

I assume that "Unit" is class in my project, but both doBefore and in {} don't return anything so I don't know whats goin on.

My doBefore's just fill some classes with random values for e.g (this one is in class that extends SpecificationWithJUnit with TestUtil with BeforeExample with AfterExample

"retrieving and counting users by criteria" should {

    var user1: User = null
    var user2: User = null
    var user3: User = null

    doBefore {
        val params = Map("login" -> "notExist", "roles" -> Seq(someRoles).asJava
        val params2 = Map("login" -> "notExistAnother", "roles" -> Seq(someRoles).asJava
        val params3 = Map("login" -> "notExistAnotherAnother", "roles" -> Seq(someRoles).asJava).asJava
        val users = Seq(params, params2, params3).map( { PopulateUser.insertUserParams(_).asInstanceOf[User] })
        user1 = users(0)
        user2 = users(1)
        user3 = users(2)
    }

I'm pretty new to Scala, but Ive read that in specs2 doBefore looks diffrent but to be honest I don't know how should I implement this in my code. I was reading this. So someone know how should I implement this in my code and whats causing that (I mean diffrence beetwen specs and specs2 is huge, but somehow few my test (beside doBefore) are sedning the same error)

Upvotes: 1

Views: 1164

Answers (2)

Eric
Eric

Reputation: 15557

The contexts in specs2 are managed differently than in specs. If you want to execute an action before a group of examples you need to create a Step:

"retrieving and counting users by criteria" should {

var user1: User = null
var user2: User = null
var user3: User = null

step {
  val params = Map("login" -> "notExist", "roles" -> Seq(someRoles).asJava
  val params2 = Map("login" -> "notExistAnother", "roles" -> Seq(someRoles).asJava
  val params3 = Map("login" -> "notExistAnotherAnother", "roles" -> Seq(someRoles).asJava).asJava
    val users = Seq(params, params2, params3).map( { PopulateUser.insertUserParams(_).asInstanceOf[User] })
  user1 = users(0)
  user2 = users(1)
  user3 = users(2)
}

"first example" >> ...

If you want to execute some code before each example you mix-in the BeforeExample trait and implement the before method.

Finally if you want to avoid using variables and pass some data to each example you can use the FixtureExample[T] trait:

class MySpec extends Specification with FixtureExample[Data] {
  def fixture[R : AsResult](f: Data => R) = {
    val data: Data = ??? // prepare data
    AsResult(f(data))
  }

  "a group of example" >> {
    "example1" >> { data: Data =>
      ok
    }
  }
}

Upvotes: 0

Andreas Neumann
Andreas Neumann

Reputation: 10904

Your test tests nothing. The last expression in your method is the return value of the method, which must be something that specs2 can convert to a Result. The last value you return is the result of do before which is Unit, which can't be converted to a test**Result**. That is the source of the error given.

could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Unit]

doBefore as you use it is fine but afterwards there should be some kind of test.

For more information look at http://etorreborre.github.io/specs2/guide/org.specs2.guide.Structure.html#Structure there is a special section desccribing how to use Before and After with Specs2 Unit and Acceptance tests.

In general you can get much gain from switching to Acceptance Test style.

Upvotes: 1

Related Questions