3
votes

I'm using specs2 (v1.8.2) on with Scala (v2.9.1) to write acceptance tests. Following the example at http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Contexts, I have the following Specification and context case class:

import org.specs2._


class testspec extends SpecificationWithJUnit { def is =

    "test should" ^
        "run a test in a context" ! context().e1

}

case class context() {

    def e1 = 1 must beEqualTo(1)
}

I get a compiler error:

error: value must is not a member of Int def e1 = 1 must beEqualTo(1)

when compiling the context case class.

Obviously I'm new to specs2 (and to Scala). References to the appropriate documentation would be greatly appreciated.

2

2 Answers

3
votes

Obviously the doc is wrong (was wrong, I fixed it now).

The correct way to write declare the case class for a context is usually to include it in the Specification scope:

import org.specs2._

class ContextSpec extends Specification { def is =
  "this is the first example" ! context().e1

  case class context() {
    def e1 = List(1,2,3) must have size(3)
  }
}

Otherwise if you want to reuse the context in another specification, you can, as Dario wrote, access the MustMatchers functionalities either by importing the MustMatchers object methods or by inheriting the MustMatchers trait.

2
votes

must is not a member of Int, because to "must" is not known in the context of your "context" class. Put the method "e1" inside your specification class and it should work. E.g.

import org.specs2._

class TestSpec extends Specification { def is =
  "test should" ^
    "run a test in a context" ! e1 ^
    end

  def e1 = 1 must beEqualTo(1)

}

edit

Ah, I see what you want ;-). This should work like this:

To have the matchers in the scope of the context class you have to import the MustMatchers.

import org.specs2._
import matcher.MustMatchers._

class ContextSpec extends Specification { def is =
  "this is the first example" ! context().e1
}

case class context() {
  def e1 = List(1,2,3) must have size(3)
}