0

There are many examples of using akka-testkit when the Actor being tested is responding to an ask:

//below code was copied from example link
val actorRef = TestActorRef(new MyActor)
// hypothetical message stimulating a '42' answer
val future = actorRef ? Say42
val Success(result: Int) = future.value.get
result must be(42)

But I have an Actor that does not respond to a sender; it instead sends a message to a separate actor. A simplified example being:

class PassThroughActor(sink : ActorRef) {
  def receive : Receive = {
    case _ => sink ! 42
  }
}

TestKit has a suite of expectMsg methods but I cannot find any examples of creating a test sink Actor that could expect a message within a unit test.

Is it possible to test my PassThroughActor?

Thank you in advance for your consideration and response.

Community
  • 1
  • 1
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125

1 Answers1

2

As mentioned in the comments you can use a TestProbe to solve this:

val sink = TestProbe()
val actorRef = TestActorRef(Props(new PassThroughActor(sink.ref)))

actorRef ! "message"

sink.expectMsg(42)
Akos Krivachy
  • 4,915
  • 21
  • 28