如何在具有构造函数params的Actor的测试类中创建TestActorRef?

时间:2015-03-10 15:14:19

标签: scala akka akka-testkit

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

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后,我得到了这个长错误跟踪......

[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

我之前遇到this问题,但在这种情况下,接受的答案对我不起作用。

如果它是相关的,这里是DoubleAuctionMarket演员的定义......

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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,因为我使用了一个配套对象将配置注入MyActor而没有明确地传递它:

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

然后我可以这样做:

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

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

TestActorRef(new DoubleAuctionMarket(google, ticker))

我会尝试删除with BasicMatchingEngine,如vptheron所说,使用构造函数而不混合任何其他东西。如果还不够,还可以尝试使用参数less actor。

这必须解决您的问题,因为只有以下问题:

TestActorRef(new DoubleAuctionMarket(google, ticker))