Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Created February 18, 2013 03:15
Show Gist options
  • Save jbrechtel/4974924 to your computer and use it in GitHub Desktop.
Save jbrechtel/4974924 to your computer and use it in GitHub Desktop.
import org.specs2._
import execute.AsResult
import mutable.Around
/* this scope is incidentally lazy since the singleton never gets constructed if code that uses it isn't executed */
object databaseScope extends Around {
def setupDB { println("#####################\nBUILDING DATABASE\n#####################") }
setupDB
def around[T: AsResult](t: => T) = AsResult(t)
}
/* this scope is explicitly lazy */
object lazyDatabaseScope extends Around {
lazy val setupDB = { println("#####################\nBUILDING DATABASE\n#####################") }
def around[T: AsResult](t: => T) = {
setupDB
AsResult(t)
}
}
class FooSpecification extends mutable.Specification {
"Some unit test" >> {
"I don't need the database" in {0 == 0}
}
}
class BarSpecification extends mutable.Specification {
"DB Stuff" >> {
"doesnt need database" in {0 == 0}
"connect to db" in lazyDatabaseScope {0 == 0}
"connect to db again" in lazyDatabaseScope { 0 == 0 }
"connect to db again" in lazyDatabaseScope { 0 == 0 }
"connect to db again" in lazyDatabaseScope { 0 == 0 }
}
}
class BazSpecification extends mutable.Specification {
"DB Stuff" >> {
"doesnt need database" in {0 == 0}
"connect to db" in lazyDatabaseScope {0 == 0}
"connect to db again" in lazyDatabaseScope { 0 == 0 }
"connect to db again" in lazyDatabaseScope { 0 == 0 }
"connect to db again" in lazyDatabaseScope { 0 == 0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment