TestActorRef:无法获得底层的演员,Nothing说

时间:2015-06-01 04:10:09

标签: scala akka scalatest akka-testkit

我正在尝试为Comment.where(...).includes(:user)

撰写我的第一个ScalaTest
Actor

所以,我做了

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)
  }
}

但我看到的是

enter image description here

为什么我不回复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) } } }

1 个答案:

答案 0 :(得分:3)

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

构造TestActorRef时需要明确说明基础actor的类型
val runner = TestActorRef[Runner](Props(new Runner(new Marathon)))

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

val runner = TestActorRef[Runner]

但是,由于Nothing实际上是每个scala类型的基类,因此底层actor在两种情况下都是相同的。因此,如果无法更改TestActorRef定义,也可以将其投放为Runner

runner.underlyingActor.asInstanceOf[Runner]