问题
The Akka Testing docs give the following way to create a TestActorRef:
import akka.testkit.TestActorRef val actorRef = TestActorRef[MyActor]
How do I extend this for testing an existing actor that takes constructor arguments? When I try running this as is, substituting in my actor class, I get the following error:
"error while creating actor
akka.actor.ActorInitializationException:Could not instantiate Actor
Make sure Actor is NOT defined inside a class/trait,
if so put it outside the class/trait, f.e. in a companion object,
OR try to change: 'actorOf(Props[MyActor]' to 'actorOf(Props(new MyActor)'."
The various ideas I could think up for adding the args after the class name inside the square brackets all crashed and burned, too.
回答1:
You could use Props like this:
val actorRef = TestActorRef(Props(new MyActor(param1, param2)))
Or factory method like this:
val actorRef = TestActorRef(new MyActor(param1, param2))
See apply
methods in object TestActorRef.
来源:https://stackoverflow.com/questions/16949348/how-do-i-create-a-testactorref-in-scala-for-an-actor-with-constructor-params