Search code examples
scalaakkatddscalatest

How do I fix this Akka unit test?


I'm coming to Scala from Python and am trying to understand test framework integration by modifying the examples in the documentation. I've pastebinned my main.scala file and maintests.scala file. The actor uses typed messages. It accepts a MoveRequest and replies with a MoveAction. IntelliJ complains that there is a type mismatch and that probe.expectMessage(character.MoveAction("north")) should be changed to a MoveRequest, which I don't understand because that is what should be probed, not expected. The compiler complains of a type mismatch. What am I doing wrong? Is there a better approach to testing?


Solution

  • The TestProbe needs to be typed to which type of messages you are sending it.

    final case class MoveRequest(message: String,  response: ActorRef[MoveAction])
    

    Here the type of messages the response actor receives is MoveAction so in your test example you need a TestProbe of type MoveAction to test messages are sent to it.

      val probe = testKit.createTestProbe[MoveAction]()
    

    As the test probe is not for the actor under test, it is for the actor in the MoveRequest message.