Giving an ActorPath for a testProbe

Hard coding actor paths is likely to make you unhappy later, wherefore you should take the difficulty of testing it as a design hint. I would recommend using ActorRef as a first choice, and only if that cannot be made to work fall back to ActorPath, but then keep it flexible by telling the actor which path to use (in a message or via the constructor).


I have developed a small library/DSL for dealing with such cases (it also allows to create more complex hierarchies and listen for messages for certain elements of path) https://github.com/stanislav-chetvertkov/spy-tree. The above case can be dealt like the following:

import com.github.spytree.ActorListenersDSL._

val myActorProbe = TestProbe()
("myActor" replyTo myActorProbe.ref).materialize

context.actorSelection("/user/myActor") ! "a message"

val response = myActorProbe.expectResponse[String]
response.message shouldBe "a message"

I figured out a non elegant solution.
I defined a helper actor called "ForwardActor"

class ForwardActor(to: ActorRef) extends Actor {
    def receive = {
        case x => to.forward(x)
    }
}

then in my test case

 val probe = TestProbe() 
 system.actorOf(Props(new ForwardActor(probe.ref)), "myActor")
 probe.expectMsg(...)