Created
July 20, 2013 20:09
-
-
Save d6y/6046264 to your computer and use it in GitHub Desktop.
MongoTestKit for Scala 2.10 and Specs 2.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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