Skip to content

Instantly share code, notes, and snippets.

@eboto
Created July 19, 2012 23:01
Show Gist options
  • Save eboto/3147468 to your computer and use it in GitHub Desktop.
Save eboto/3147468 to your computer and use it in GitHub Desktop.
Demonstrates using multiple traits to mix in multiple "Before" hooks, with an intended usage in creating re-usable and composable execution contexts for test cases.
//
// A demonstration of using multiple traits to mix in multiple "Before" hooks,
// with an intended usage in test code.
//
// The demonstration shows that multiple traits inheriting BeforeEach (or more
// germanely scalatest's BeforeAndAfterEach) can be sufficiently composable to use
// in our testing toolchain without sewing our own strait jacket.
//
object Main {
def main(args: Array[String]) {
println("Both of these should print the same thing.")
println("Output from test class that clears persistence separately:")
new MyTestsThatAnnihilatePersistencesOneMixinAtATime().beforeEach
println("")
println("Output from test class that clears persistence in one mixin:")
new MyTestsThatAnnihiliateAllPersistencesWithOneMixin().beforeEach
/* Both lines print:
* Clearing Validation
* Clearing Cache
* Clearing Database
*/
}
}
class MyTestsThatAnnihilatePersistencesOneMixinAtATime
extends ClearDatabaseBefore
with ClearCacheBefore
with ClearValidationBefore
class MyTestsThatAnnihiliateAllPersistencesWithOneMixin extends ClearAllPersistence
trait ClearAllPersistence
extends ClearDatabaseBefore
with ClearCacheBefore
with ClearValidationBefore
trait ClearDatabaseBefore extends BeforeEach {
override def beforeEach {
println("Clearing Database")
super.beforeEach()
}
}
trait ClearCacheBefore extends BeforeEach {
override def beforeEach {
println("Clearing Cache")
super.beforeEach()
}
}
trait ClearValidationBefore extends BeforeEach {
override def beforeEach {
println("Clearing Validation")
super.beforeEach()
}
}
trait BeforeEach {
def beforeEach() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment