我有一个 Controller 类,它控制发送到注入(inject) Controller 的 Akka Actor 的请求。
Controller 代码:
class Controller(actor: ActorRef) {
def control(msg: String): Future[String] = {
actor.ask(msg)(Timeout(2 seconds)).mapTo[String]
}
}
我的 Actor 的代码是:
class ActorA extends Actor {
override def receive: Receive = {
case msg: String => sender ! msg
case msg: Int => sender ! msg.toString
case _ => "Invalid command!"
}
现在我需要模拟 ActorA 的行为来对 Controller 进行单元测试。有没有办法通过 Akka TestKit 做到这一点?
最佳答案
使用 TestProbe
.来自 testing documentation :
val probe = TestProbe()
val future = probe.ref ? "hello"
probe.expectMsg(0 millis, "hello")
probe.reply("world")
assert(future.isCompleted && future.value == Some(Success("world")))
关于scala - 如何模拟 Akka Actor 来对类(class)进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265013/