Problems using implicit Around context

25 views
Skip to first unread message

Jeppe Nejsum Madsen

unread,
Jan 10, 2013, 9:46:01 AM1/10/13
to specs2...@googlegroups.com
Hi,

Using specs 0.12.3 (can't seem to find 0.12.4 even though it's mentioned?) I have some problems using Around in an implicit context:

I'm trying to create a generic DB trait like this:

trait SquerylSpecification extends BeforeExample {
  self: org.specs2.mutable.Specification =>
  args(sequential=true)
 
  implicit object tx extends Around {
    def around[T <% Result](t: =>T): Result =  {
        println("Start tx")
        val res = t 
        println("End tx")
        res
    }
  }
 
  def openDb = println("openDB")
  def closeDb = println("closeDb")
  def before =  println("clean data")
 
  override def map(fs: =>Fragments) = {Step(openDb) ^ fs ^ Step(closeDb)}
}

and using it:

@RunWith(classOf[JUnitRunner])
class VehicleViewComponentSpec extends Specification with SquerylSpecification {
  "the first" >> {
    "test 11" in {
      println("first 1")
      1 must_== 1
    }
    "test 12" in {
      println("first 2")
      1 must_== 1
    }
  }
  "the second " >> {
    "test 21" in tx {
      println("second 1")
      1 must_== 1
    }
    "test 22" in tx {
      println("second 2")
      1 must_== 1
    }
  }
}


In the first specification the tx object should be passed implicitly and in the second, it is explicitly specified. But only the last method seems to yield the expected output (where the example is executed within a tx boundary):

openDB
clean data
first 1
Start tx
End tx
clean data
first 2
Start tx
End tx
clean data
Start tx
second 1
End tx
clean data
Start tx
second 2
End tx
closeDb

Any hints?

/Jeppe



etorreborre

unread,
Jan 10, 2013, 4:32:32 PM1/10/13
to specs2...@googlegroups.com
Hi,

You cannot mix implicit contexts and XXXExample traits. The fix is to add the AroundExample trait and define the around method:

trait SquerylSpecification extends BeforeExample with AroundExample {  self: mutable.Specification =>
  
  sequential
  
  def around[T <% Result](t: =>T) = {
     println("Start tx")
     val res = t  
     println("End tx")
     res
  }
  
.....
}

Cheers,

Eric.

Jeppe Nejsum Madsen

unread,
Jan 10, 2013, 4:44:30 PM1/10/13
to specs2...@googlegroups.com
On Thu, Jan 10, 2013 at 10:32 PM, etorreborre <etorr...@gmail.com> wrote:
Hi,

You cannot mix implicit contexts and XXXExample traits. The fix is to add the AroundExample trait and define the around method:


Ok. This wasn't obvious (to me at least ;-) from the docs
 
trait SquerylSpecification extends BeforeExample with AroundExample {  self: mutable.Specification =>
  
  sequential
  
  def around[T <% Result](t: =>T) = {
     println("Start tx")
     val res = t  
     println("End tx")
     res
  }
  
.....
}
 
That'll do nicely, thanks :-)


/Jeppe
Reply all
Reply to author
Forward
0 new messages