我有一个Controller类,它控制发送给Akka actor的请求,该请求被注入控制器.
控制器代码:
class Controller(actor: ActorRef) { def control(msg: String): Future[String] = { actor.ask(msg)(Timeout(2 seconds)).mapTo[String] } }
我的演员代码是:
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
.从测试文档:
val probe = TestProbe() val future = probe.ref ? "hello" probe.expectMsg(0 millis, "hello") probe.reply("world") assert(future.isCompleted && future.value == Some(Success("world")))