TestActorRef: Failed to get base agent, nothing is said
I am trying to write my first one ScalaTest
for the followingActor
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)
}
}
So i do
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)
}
}
}
but i see
Why don't I return it Runner Actor
?
source to share
Since it is Props
untyped, it doesn't know what type of actor (in your case Runner
) it will build. Hence, it TestActorRef
also cannot deduce the type. This is why you need to explicitly specify the type of the main actor when constructing TestActorRef
with
val runner = TestActorRef[Runner](Props(new Runner(new Marathon)))
If your actor doesn't require any parameters, this can even be shortened to
val runner = TestActorRef[Runner]
However, since it Nothing
is actually the base class of each scala type, the base actor is the same in both cases. Therefore, if a change in definition TestActorRef
was not possible, it could also be distinguished in order to obtain Runner
.
runner.underlyingActor.asInstanceOf[Runner]
source to share