gpt4 book ai didi

scala - TestActorRef : Could not get the underlyingActor, 什么也没说

转载 作者:行者123 更新时间:2023-12-01 23:42:45 28 4
gpt4 key购买 nike

我正在尝试为以下 Actor 编写我的第一个 ScalaTest

object Runner {
def props(race: Race) = Props(classOf[Runner], race)
}

class Runner(race: Race) extends Actor with ActorLogging {

import context.dispatcher

@throws[Exception](classOf[Exception])
override def postRestart(reason: Throwable): Unit = context.parent ! RestartRunner

override def receive: Receive = LoggingReceive {
case Start => {
log.debug("running...")
for (i <- 1 to 3) {
Thread.sleep(200)
}
throw new RuntimeException("MarathonRunner is tired")
}

case StartWithFuture =>
log.debug("I am starting to run")
race.start pipeTo self

case Failure(throwable) => throw throwable

case Stop =>
log.debug("stopping runner")
context.stop(self)
}
}

所以,我愿意

import akka.actor.{Props, ActorSystem}
import akka.testkit.{TestActorRef, TestKit}
import org.scalatest._

class RunnerSpec extends TestKit(ActorSystem("test"))
with WordSpecLike
with MustMatchers {
"A Runner Actor" must {
val runner = TestActorRef(Props(new Runner(new Marathon)))
"receive messages" in {
runner ! Start
runner.under <== says Nothing (see attachment)
}
}
}

但我看到的是

enter image description here

为什么我不找回 Runner Actor

最佳答案

由于 Props 是未类型化的,它不知道将构建哪种类型的 actor(在您的例子中是 Runner)。因此,TestActorRef 也无法推断类型。这就是为什么在使用

构建 TestActorRef 时需要显式声明底层 actor 的类型的原因
val runner = TestActorRef[Runner](Props(new Runner(new Marathon)))

如果您的 Actor 不需要任何参数,这甚至可以缩短为

val runner = TestActorRef[Runner]

但是,由于 Nothing 实际上是每个 Scala 类型的基类,所以在这两种情况下,底层参与者都是相同的。因此,如果无法更改 TestActorRef 定义,也可以强制转换为生成 Runner

runner.underlyingActor.asInstanceOf[Runner]

关于scala - TestActorRef : Could not get the underlyingActor, 什么也没说,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565509/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com