Skip to content

Instantly share code, notes, and snippets.

@d6y
Created July 20, 2013 20:09
Show Gist options
  • Select an option

  • Save d6y/6046264 to your computer and use it in GitHub Desktop.

Select an option

Save d6y/6046264 to your computer and use it in GitHub Desktop.
MongoTestKit for Scala 2.10 and Specs 2.1
/**
For use with...
scalaVersion := "2.10.2"
val liftVersion = "2.5.1"
"net.liftweb" %% "lift-webkit" % liftVersion % "compile",
"net.liftweb" %% "lift-mongodb-record" % liftVersion,
"org.specs2" %% "specs2" % "2.1" % "test"
*/
package code.model
import net.liftweb.http.{Req, S, LiftSession}
import net.liftweb.util.StringHelpers
import net.liftweb.common.Empty
import net.liftweb.mongodb._
import com.mongodb.ServerAddress
import com.mongodb.Mongo
import org.specs2.mutable.Around
import org.specs2.execute.{AsResult, ResultExecution, Result}
trait MongoTestKit {
val server = new Mongo(new ServerAddress("127.0.0.1", 27017))
def dbName = "test_"+this.getClass.getName
.replace(".", "_")
.toLowerCase
def initDb() : Unit = MongoDB.defineDb(DefaultMongoIdentifier, server, dbName)
def destroyDb() : Unit = {
MongoDB.use(DefaultMongoIdentifier) { d => d.dropDatabase() }
MongoDB.close
}
trait TestLiftSession {
def session = new LiftSession("", StringHelpers.randomString(20), Empty)
def inSession[T](a: => T): T = S.init(Req.nil, session) { a }
}
object MongoContext extends Around with TestLiftSession {
def around[T : AsResult](testToRun: =>T) : Result = {
initDb()
try {
inSession {
ResultExecution.execute(AsResult(testToRun))
}
} finally {
destroyDb()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment