问题描述
提供了以下创建方式一个TestActorRef:
The Akka Testing docs give the following way to create a TestActorRef:
import akka.testkit.TestActorRef
val actorRef = TestActorRef[MyActor]
如何扩展此功能以测试采用构造函数参数的现有actor?当我尝试按原样运行它,替换为actor类时,出现以下错误:
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:
在创建actor
akka.actor.ActorInitializationException时出错:无法实例化Actor
确保没有在类/特征内定义Actor,
如果将其放在类/特征外,例如在同伴对象中,则
或尝试更改:将'actorOf(Props [MyActor]')更改为'actorOf(Props(new MyActor)'。
"error while creating actorakka.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)'."
我的各种想法也可以考虑在方括号内的类名之后添加args都崩溃并烧毁。
The various ideas I could think up for adding the args after the class name inside the square brackets all crashed and burned, too.
推荐答案
像这样:
val actorRef = TestActorRef(Props(new MyActor(param1, param2)))
或像这样的工厂方法:
val actorRef = TestActorRef(new MyActor(param1, param2))
请参见对象apply 方法/api/akka/2.1.4/#akka.testkit.TestActorRef%24> TestActorRef
。
See apply
methods in object TestActorRef
.
这篇关于如何在Scala中为具有构造函数参数的Actor创建TestActorRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!