1

如何在测试类中创建 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

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

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

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

1 回答 1

1

我遇到了同样的问题,因为我使用伴随对象将配置注入 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 BasicMatchingEnginevptheron 所说的,使用构造函数而不混合其他任何东西。如果这还不够,也可以尝试使用更少的参数。

这必须解决您的问题,因为仅以下内容没有问题:

TestActorRef(new DoubleAuctionMarket(google, ticker))
于 2016-04-29T18:09:08.850 回答