mercredi 25 novembre 2015

Specs2 ForEach context with execution env

I'm executing my unit tests with specs2 library. At the moment I'm using ForEach context to inject my database into unit tests like this example:

trait DatabaseContext extends ForEach[Transaction] {
  // you need to define the "foreach" method
  def foreach[R: AsResult](f: Transaction => R): Result = {
    val transaction = openDatabaseTransaction
    try AsResult(f(transaction))
    finally closeDatabaseTransaction(transaction)
  }

  // create and close a transaction
  def openDatabaseTransaction: Transaction = ???

  def closeDatabaseTransaction(t: Transaction) = ???
}

That approach let me write my tests like this:

"test 1" >> { t: Transaction =>
    println("use the transaction")
    ok
}

My problem is that I need also the ExecutionEnv in my test because I'm using specs2 future matchers expectations.

"test 1" >> { implicit env: ExecutionEnv =>
   Future(1) must be_==( 0 ).await
}

How can I combine both approaches so that I can get access to the execution env in my test using ForEach? I've already tried using this code but it does not compile

"test 1" >> { implicit env: ExecutionEnv =>
    new DatabaseContext { t: Transaccion =>
      myFuture must be_==( "some result" ).await
    }
}

I'll appreciate any suggestions. Thank you all very much

Aucun commentaire:

Enregistrer un commentaire