Canonical example of how to test Akku Actor with Scala in the Play Framework

Having created the actor Akki in the Play Framework, I now want to test it. However, I immediately ran into a problem:

  • The current Play Scala Testing page does not contain anything about participant testing and uses Specs2 for all examples.
  • In the test play examples 2.2.1 or in the samples (which also use Specs2) I could not find test examples for the actors.
  • The Akka page of an actor page uses ScalaTest, and the Akka system setting is different from that used by the Play application itself.
  • Akki's acting test discusses workarounds for problems using Specs2, but without providing a processed example of such a test and, of course, does not use Play's built-in test devices.

Can anyone provide a canonical example of testing Akka chords using the TestKit and Play test devices?

To ensure consistency, I would rather use Specs2 (to be honest, it seems strange to require two different testing frameworks for the same application), however I will take the ScalaTest example if it integrates well with Play test devices.

+7
akka playframework scalatest specs2
source share
1 answer

There is nothing special about this, you basically just have tests that look like actor samples, but use specs2 test syntax instead. If you want to use akka verification utilities, you will have to add an explicit dependency on it from the build.sbt file

libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-testkit" % "2.2.0" % "test" ) 

Then there is nothing special, except that you cannot inherit TestKit in a test class, since Specification is also a class and there are name classes. This is an example of how you can use custom Scope to use TestKit:

 import org.specs2.specification.Scope class ActorSpec extends Specification { class Actors extends TestKit(ActorSystem("test")) with Scope "My actor" should { "do something" in new Actors { val actor = system.actorOf(Props[SomeActor]) val probe = TestProbe() actor.tell("Ping", probe.ref) probe.expectMsg("Pong") } "do something else" in new Actors { new WithApplication { val actor = system.actorOf(Props[SomeActorThatDependsOnApplication]) val probe = TestProbe() actor.tell("Ping", probe.ref) probe.expectMsg("Pong") }} } } 
+3
source share

All Articles