如何在具有构造函数参数的Actor的测试类中创建TestActorRef? [英] How to create a TestActorRef inside a test class for an Actor with constructor params?

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

问题描述

如何在测试类中创建TestActorRef。具体来说,我具有以下测试设置...

How does one create a TestActorRef inside a test class. Specifically, I have the following test set up...

class MatchingEngineSpec extends TestKit(ActorSystem("Securities-Exchange"))
  with FeatureSpecLike
  with GivenWhenThen
  with Matchers {

  val google = Security("GOOG")

  val ticker = Agent(Tick(google, None, None, None))

  val marketRef = TestActorRef(new DoubleAuctionMarket(google, ticker) with BasicMatchingEngine)

  val market = marketRef.underlyingActor

...当我运行测试时,一切都通过了,但是在关闭ActorSystem之后,我得到了很长的错误跟踪。 ..

...when I run the tests everything passes, but after shutting down the ActorSystem I get this long error trace...

[ERROR] [03/10/2015 15:07:55.571] [Securities-Exchange-akka.actor.default-dispatcher-4] [akka://Securities-Exchange/user/$$b]     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)'.
akka.actor.ActorInitializationException: exception during creation

我遇到了,但是在这种情况下,可接受的答案对我不起作用。

I came across this previous question, but the accepted answer didn't work for me in this case.

在适当的情况下,这是 DoubleAuctionMarket 参与者的定义...

In case it is relevant, here is the definition of the DoubleAuctionMarket actor...

class DoubleAuctionMarket(val security: Security, val ticker: Agent[Tick]) extends Actor with ActorLogging {
  this: MatchingEngine =>
  ...


推荐答案

我遇到了同样的问题,因为我当时使用同伴对象将配置注入MyActor而不显式地传递它:

I had the same issue because I was using a companion object to inject the config into MyActor without passing it explicitly:

object MyActor {
  def apply(): MyActor = new MyActor(MyActorConfig.default)
  val props = Props(new MyActor(MyActorConfig.default))
}

然后我可以做:

val myActorRef = system.actorOf(MyActor.props, "actorName")

错误与在此处的测试中显式传递参数有关:

The error is related to passing the arguments explicitly in the test here:

TestActorRef(new DoubleAuctionMarket(google, ticker))

我会尝试像vptheron所说的那样,使用BasicMatchingEngine 删除,使用构造函数而不混合其他内容。

I would try to remove the with BasicMatchingEngine as vptheron said, use the constructor without mixing anything else. Try also with an argument less actor if that is not enough.

这必须解决您的问题,因为仅仅存在以下问题就不会解决问题:

That must fix your problem since there are no issues with just:

TestActorRef(new DoubleAuctionMarket(google, ticker))

这篇关于如何在具有构造函数参数的Actor的测试类中创建TestActorRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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