我正在尝试编写我的第一个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)
}
}
所以,我愿意
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)))
如果您的演员不需要任何参数,甚至可以将其缩短为
val runner = TestActorRef[Runner]
但是,由于
Nothing
实际上是每种scala类型的基类,因此在两种情况下,底层actor都是相同的。因此,如果不可能更改TestActorRef
定义,也可以将其强制转换为Runner
。runner.underlyingActor.asInstanceOf[Runner]