如何在Scala中为具有构造函数参数的Actor创建TestActorRef? [英] How do I create a TestActorRef in Scala for an Actor with constructor params?

查看:99
本文介绍了如何在Scala中为具有构造函数参数的Actor创建TestActorRef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Akka测试文档提供了以下创建方式一个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 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)'."

我的各种想法也可以考虑在方括号内的类名之后添加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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆