Specs2 wrap unit test in database transaction -
i'm updating application specs2 2.3.12
3.6.1
, having trouble updating class wraps our unit tests in transaction.
the 2.3.12
class:
class dbtx extends aroundoutside[session] { var session: option[session] = none def around[t : asresult](t: => t) = { db.withtransaction { implicit s => session = some(s) val result = asresult(t) s.rollback() result } } def outside: session = session.get }
its usage:
"my unit test" in (new dbtx).apply { implicit session: session => ... }
what i've tried in 3.6.1
class dbtx extends foreach[session] { var session: option[session] = none def foreach[t : asresult](t: session => t) = { db.withtransaction { implicit s => session = some(s) val result = asresult(t) s.rollback() result } } }
its usage:
"my unit test" in (new dbtx).foreach { implicit session: session => ... }
but seemed produce infinite loop between lines 6 & 4 of block.
i tried
class dbtx extends around { def around[t: asresult](t: => t): result = { super.around { db.withtransaction { implicit s: session => val result = asresult(t) s.rollback() result } } } }
its usage:
"my unit test" in (new dbtx).around { implicit session: session => ... }
but results in
could not find implicit value evidence parameter of type asresult[session => matchresult[ ... ]]
i tried
class dbtx extends fixture[session] { def apply[t: asresult](t: session => t): result = { db.withtransaction { implicit s: session => val result = asresult(t) s.rollback() result } } }
its usage:
"my unit test" in (new dbtx) { implicit session: session => ... }
which results in
could not find implicit value evidence parameter of type asresult[session => t]
edit
i'm getting infinite loop code:
import org.specs2.execute.asresult import org.specs2.mutable.specification import org.specs2.specification.foreach class dbtxspec extends specification foreach[session] { def foreach[t: asresult](t: session => t) = { db.withtransaction { implicit s => // infinite loop between here try asresult(t) // , here s.rollback() } } "my unit test" in { implicit session: session => true must betrue } }
if want pass in session
need use have specification extend foreach
trait, not special object. like:
class dbtxspec extends specification foreach[session] { var session: option[session] = none def foreach[t : asresult](t: session => t) = { db.withtransaction { implicit s => session = some(s) try asresult(t(session)) s.rollback() } } "my unit test" in { implicit session: session => ... } }
Comments
Post a Comment