Specs2 wrap unit test in database transaction

独自空忆成欢 提交于 2019-12-25 04:17:27

问题


I'm updating my application from Specs2 2.3.12 to 3.6.1 and am having trouble updating the class which wraps our unit tests in a 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 this seemed to produce an infinite loop between lines 6 & 4 of that block.

I also 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 that results in

could not find implicit value for evidence parameter of type AsResult[Session => MatchResult[ ... ]]

I also 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 for evidence parameter of type AsResult[Session => T]

Edit

I'm also getting an infinite loop with this code:

import org.specs2.execute.AsResult
import org.specs2.mutable.Specification
import org.specs2.specification.ForEach

class DbTxSpec extends Specification with ForEach[Session] {
  def foreach[T: AsResult](t: Session => T) = {
    Db.withTransaction { implicit s =>  // infinite loop between here
      try AsResult(t)                   // and here
      finally s.rollback()
    }
  }

  "my unit test" in { implicit session: Session =>
    true must beTrue
  }
}

回答1:


If you want to pass in a Session you need to use have your specification extend the ForEach trait, not a special object. Something like:

class DbTxSpec extends Specification with 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))
      finally s.rollback()
    }
  }

  "my unit test" in { implicit session: Session =>
    ...
  }
}


来源:https://stackoverflow.com/questions/30839582/specs2-wrap-unit-test-in-database-transaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!