testing an actor who creates child actors

You need to mock the child actor if you want to fully test the Parent actor, so you can’t create the child actor directly in the parent actor. Instead, pass a factory method to the parent actor (dependency injection):

class ParentActor(makeChild: () => ChildActor) extends Actor {
  case class CreateChildren(count: Int)

  override def receive: Receive = {
    case CreateChildren(count) => for (_ <- 0 until count) makeChild() 
  }
}

The makeChild function can count the number of actors that are created. It can also return a mock version of ChildActor that implements test behaviour to stress the ParentActor.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top