作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为以下 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)
}
}
}
但我看到的是
为什么我不找回 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/
我正在尝试为以下 Actor 编写我的第一个 ScalaTest object Runner { def props(race: Race) = Props(classOf[Runner], ra
我是一名优秀的程序员,十分优秀!