How do I create a TestActorRef in Scala for an Actor with constructor params?

梦想的初衷 提交于 2019-12-04 16:40:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!