I have some code that sends a message to an actor using it's ActorPath not ActorRef
system.actorSelection("/user/myActor") ! "a message"
I need to make sure that this message is really sent. So I need in my tests to create a testProbe (or something similar) that lives in this actor path, so that it receives those message.
val probe = TestProbe()
val system.actorOf(probe.ref, "myActor") // ??? I want to give my probe name and path
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(...)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With