本文介绍了如何模拟Akka演员对课程进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Controller类,用于控制发送到注入到控制器中的Akka actor的请求。
I have a Controller class which controls the requests sent to a Akka actor which is injected in to the controller.
Controller的代码:
Controller's code:
class Controller(actor: ActorRef) {
def control(msg: String): Future[String] = {
actor.ask(msg)(Timeout(2 seconds)).mapTo[String]
}
}
我的Actor的代码是:
And my Actor's code is:
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做到这一点?
Now I need to mock ActorA's behaviour to unit test Controller. Is there a way to do so via Akka TestKit ?
推荐答案
使用。从:
val probe = TestProbe()
val future = probe.ref ? "hello"
probe.expectMsg(0 millis, "hello")
probe.reply("world")
assert(future.isCompleted && future.value == Some(Success("world")))
这篇关于如何模拟Akka演员对课程进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!