Skip to content

Instantly share code, notes, and snippets.

@leon
Created October 8, 2012 06:35
Show Gist options
  • Save leon/3851053 to your computer and use it in GitHub Desktop.
Save leon/3851053 to your computer and use it in GitHub Desktop.
Akka Specs2 Context
import org.specs2.mutable._
import org.specs2.time.NoTimeConversions
import akka.actor._
import akka.testkit._
import akka.util.duration._
/* A tiny class that can be used as a Specs2 'context'. */
abstract class AkkaTestkitSpecs2Support extends TestKit(ActorSystem())
with After
with ImplicitSender {
// make sure we shut down the actor system after all tests have run
def after = system.shutdown()
}
/* Both Akka and Specs2 add implicit conversions for adding time-related
methods to Int. Mix in the Specs2 NoTimeConversions trait to avoid a clash. */
class ExampleSpec extends Specification with NoTimeConversions {
sequential // forces all tests to be run sequentially
"A TestKit" should {
/* for every case where you would normally use "in", use
"in new AkkaTestkitSpecs2Support" to create a new 'context'. */
"work properly with Specs2 unit tests" in new AkkaTestkitSpecs2Support {
within(1 second) {
system.actorOf(Props(new Actor {
def receive = { case x ⇒ sender ! x }
})) ! "hallo"
expectMsgType[String] must be equalTo "hallo"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment