For example, I have two actors - a parent actor and a child actor. When a parent receives a message, it spawns as many child actors as specified in the message. How can I test this functionality? Is there a way to mock the context or some other way to check that the actors are created correctly and in the right amount?
class ParentActor extends Actor {
case class CreateChildren(count: Int)
override def receive: Receive = {
case CreateChildren(count) => for (_ <- 0 until count) context.actorOf(Props[ChildActor])
}
}
class ChildActor extends Actor {
override def receive: Receive = {
case _ =>
}
}
Updated: Solution based on @Tim answer
Changed classes:
class ParentActor(childActorFactory: ChildActorFactory) extends Actor {
override def receive: Receive = {
case CreateChildren(count) => for (_ <- 0 until count) childActorFactory.create(context)
}
}
object ParentActor {
def props(childActorFactory: ChildActorFactory): Props = Props(new ParentActor(childActorFactory))
}
class ChildActorFactory {
def create(context: ActorContext): ActorRef = context.actorOf(Props[ChildActor])
}
Test:
"ParentActor" should {
"instantiate ten child actors" in {
val childrenCount = 10
val childActorFactory = mock[ChildActorFactory]
val parentActor = TestActorRef[ParentActor](ParentActor.props(childActorFactory))
parentActor ! CreateChildren(childrenCount)
Mockito.verify(childActorFactory, Mockito.times(childrenCount))
.create(parentActor.underlyingActor.context)
}
}