lundi 20 avril 2015

Wrapping a test in a transaction

Ok so this is what I want to do:

Assuming I have several tests that do something like this

"should do something with the database" in new WithApplication {
    DB.withTransaction { implicit con =>
        // Query the database.
        // Perform my tests.

        // Rollback the transaction to return database to initial state

I don't want to remember to wrap all my database tests in a transaction and call con.rollback() at the end of each test manually.

Instead I tried writing a trait that wraps the entire test in a transaction so I could just write my tests like this

"do something to the database" in new WithApplication with DatabaseTest {

This is what I have so far

trait DatabaseTest extends Around with Scope {
    abstract override def around[T: AsResult](test: => T): Result = {
        super.around = {
            import play.api.Play.current

            DB.withTransaction { implicit con =>
                val result = test

However when I try running the test above I get this error

could not find implicit value for parameter connection: java.sql.Connection

So my question is whether what I'm trying to do is possible, and if so, what's is wrong with my code and what would I have to change to get it to work.


Aucun commentaire:

Enregistrer un commentaire