<分区>
我正在尝试为依赖于 Future 的 actor 编写一个简单的测试。这是几乎不言自明的代码
import akka.actor.{ Actor, ActorSystem, Props }
import akka.testkit.{ ImplicitSender, TestKit }
import org.scalatest.{ BeforeAndAfterAll, WordSpecLike }
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
trait Provider {def get(s: String): Future[String] }
class MyActor(provider: Provider) extends Actor {
override def receive: Receive = {
case s: String ⇒
provider.get(s) map { result: String ⇒
sender() ! result
}
}
}
class FutureTest
extends TestKit(ActorSystem("Test"))
with ImplicitSender
with WordSpecLike
with BeforeAndAfterAll {
"MyActor" must {
"wait for the future" in {
val myref = system.actorOf(Props(
new MyActor((s) ⇒ Future { s })
))
myref ! "hello world"
expectMsg("hello world")
}
}
}
Expect msg 不会等待 future 完成并失败并显示以下消息:
断言失败:WAITING hello world 时 expectMsg 超时(3 秒)
java.lang.AssertionError: assertion failed: expectMsg 等待 hello world 期间超时(3 秒)
在我的案例中测试这种行为的最佳实践是什么?
谢谢!