You have an actor that uses one or more child actors. You want to inject a TestProbe in
place of the child(ren) to test the behaviour of the parent in isolation. If your actor
takes another actor as a constructor parameter, this is simple. But what if it does not?
Or, you can simply send your actor a message containing the probe as this example from
the Akka documentation shows. However, if your actor creates children itself
it can get more difficult. Here’s an example setup from my (admittedly rather limited)
experience with Akka so far:
This lets me inject a stubbed-out Foo instance in a test, but if you lump too much
behaviour in a single stub it can get very complex. Much as in non-actor code I would use
Mock objects rather than stubs if I can, I would prefer to use TestProbes where possible.
For a while I struggled to see how I could inject a test probe into this second Bar. How
would I implement the
newFoo method in my TestChildrenProvider? I could not just return
ref on the TestProbe this time, as that returns an ActorRef, not an Actor. Turns out
the solution is to add yet another abstraction. (I should have known.)
Inspired by the Forwarding Messages Received by Probes section of the Akka TestKit
docs, I realised that I could use a similar technique, and create a wrapper around
ref as a constructor parameter:
This TestCase trait can then be wrapped around your code under test like so:
This makes it much easier to reason about each test in isolation, and I’m less likely to
break other tests when adding data or behaviour to the stub to facilitate other tests. It
also makes it easier to add another test by copying and slightly tweaking an existing one.
(You should normally strive to DRY in code, but I believe some duplication is OK in tests.)