I'm trying to unit test a helper function of my actor. This helper function spawns some actors and I want to assert that this happens. In order to spawn them, the helper function needs the parent ActorContext, so that is passed in. Example:
def getDefaultInitialState(context: ActorContext[Request]): State = {
...
val actor1 = context.spawn(myChildActor(), name = f"MyChild1")
...
State(actor1, ...)
}
So, now I'm trying to unit test this function. In order to call it from my unit test, I need an ActorContext[Request]. I tried doing this in my test:
val testKit = BehaviorTestKit(MyParentActor()))
getDefaultInitialState(testKit.context())
Unfortunately, this doesn't work because the context() method of BehaviorTestKit is private.
How can I make this work?
Based on akka actor typed docs, you can also expect an effect in your test kit, which in your case, spawning an actor indeed is an effect. so I think this should probably help (abstraction of akka docs provided previously):
Alternatively, if your actors do not have a name, or you don't know the exact names, you can just expect anonymous actors only based on their behavior: