1

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)

2 Answers2

1

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.

Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52
  • there are tests below doBefore{} I just didnt think I should put them in my question. Thanks for an answer and link will check it out. – user3519173 Apr 28 '14 at 10:04
0

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
    }
  }
}
Eric
  • 15,494
  • 38
  • 61
  • thanks for an answer! step seems really nice cause I don't have only data's in my doBefore methods (only in this example), but what should I do when I have doAfter? I mean 'step' wont work here, right? So what should I do instead? (didnt ask in this question cause Ive just noticed that i have doAfter here too) – user3519173 Apr 28 '14 at 10:12
  • you can use a `step` after all your examples as well and the code will only be executed once all of your examples are finished. – Eric May 01 '14 at 05:08
  • yeah that i figured out, but how can I achieve somethink like this: I have 5 tests and I want after every single one do somethink without writing step after those tests? – user3519173 May 06 '14 at 14:05
  • For this you need to mix-in the `AfterExample` trait and implement the `after` method. – Eric May 06 '14 at 22:29