【发布时间】:2015-03-10 15:14:19
【问题描述】:
如何在测试类中创建 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 =>
...
【问题讨论】:
-
你的
DoubleAuctionMarket演员是如何定义的? -
我已更新问题以包含有关 DoubleAuctionMarket 参与者的信息。
-
你试过没有
BasicMatchingEngine的'on the fly' mixin吗?我认为您的代码和TestActorRef的使用没有任何问题。 -
抱歉,但我对 Scala 和 Akka 都是新手。 “没有'on the fly' mixin”到底是什么意思?
-
这种自我类型引用限制(
this:MatchingEngine =>)通常保留给特征,以确保它们只混合到某些类型的类中。你想通过DoubleAuctionMarket上的设置来完成什么?在课堂上这样做会迫使您在实例化时进行动态混合,我认为这是您问题的根源。
标签: scala akka akka-testkit