playframeworkplayframework-2.0akkatestkit

Testing play controller's interaction with an akka actor


My play application uses an akka actor to handle a long running computation:

class MyController(myActor : ActorRef) extends Controller{
  def doStuff = Action { implicit request =>

    val response : Future[Any] = myActor ? DoStuff

    Async{
      response.map{        
        str : String => Ok(str)
      }
    }
  }
}

I am trying to test that things are working properly. I have separate tests for checking that the actor behaves properly and mostly just want to check that the controller sends the right msgs to the actor. My current approach is kind of like this:

class MyControllerSpec extends Specification{
  "MyController" should {

    object DummyActor extends Actor{
      def receive = {
        case _ => ()
      }
    }

    "do stuff properly" >> {
       val probe = TestProbe()(Akka.system)
       val test = new controllers.MyController(Akka.system.actorOf(Props(DummyActor))
       val result = test.doStuff(FakeRequest())
       probe.expectMsg(SomeMsg)
    }
  }
}

The controller will send a message to the passed in actor when the doStuff action is called. I am trying to verify that the right msg is sent.

I think test.doStuff is run synchronously and times out when the dummy actor doesn't send anything. The expectMsg doesn't start until after the doStuff call returns and SomeMsg was already sent. How can I solve this problem?


Solution

  • Isn't what you want to pass the probe to your controller rather than a dummy actor implementation, how would something be sent to the probe if not?