The specs2 tests are performed by default in parallel which may cause problems with accessing databases, especially when you rely on the db contents provided by a previous test. So to force sequential testing you have to tell specs2 to do so:
class ModelSpec extends Specification with Logging {
override def is = args(sequential = true) ^ super.is
...
}
For tests done in one FakeApplication
you can wrap the whole tests in it:
running(FakeApp) {
log.trace("Project tests.")
val Some(project) = Project.findByName("test1")
"Project" should {
"be retrieved by name" in {
project must beAnInstanceOf[Project]
project.description must endWith("project")
}
The whole sample can be found here. That was my first attempt to deal with problems while testing MongoDB with Play! framework.
The second approach I borrowed from the salat project, which is by the way a very good source of specs examples dealing with MongoDB (although it is not a Play! framework app). You have to define a trait extending Around
and Scope
, where you can put anything you need to be initialized in an application instance:
import org.specs2.mutable._
import org.specs2.execute.StandardResults
import play.api.mvc._
import play.api.mvc.Results
import play.api.test._
import play.api.test.Helpers._
trait FakeApp extends Around with org.specs2.specification.Scope {
val appCfg = Map(
"first.config.key" -> "a_value",
"second.config.key" -> "another value"
)
object FakeApp extends FakeApplication(
additionalPlugins = Seq("com.github.rajish.deadrope.DeadropePlugin"),
additionalConfiguration = appCfg
) {
// override val routes = Some(Routes)
}
def around[T <% org.specs2.execute.Result](test: => T) = running(FakeApp) {
Logger.debug("Running test ==================================")
test // run tests inside a fake application
}
}
Edit 2013-06-30:
In the current version of specs2
the around
signature should be:
def around[T : AsResult](test: => T): Result
End of edit
Then a test can be written like that:
class SomeSpec extends Specification { sequential // according to @Eric comment
"A test group" should {
"pass some tests" in new FakeApp {
1 must_== 1
}
"and these sub-tests too" in {
"first subtest" in new FakeApp {
success
}
"second subtest" in new FakeApp {
failure
}
}
}
}
A full sample of such suite can be found here.
On a final note: It's also good to clean up the test database before starting a suite:
step {
MongoConnection().dropDatabase("test_db")
}