【发布时间】:2015-01-07 02:21:31
【问题描述】:
我遇到了 Future 的问题,该问题不是从使用 whenReady 的 ScalaTest 案例中的参与者返回的。
这是测试用例
"A PolicyHolderDAO Actor" must {
"return a failure if given a policy holder to update without an id" in {
val updatePolicyHolderFailureAny : Future[Any] = policyHolderDAOActor ? PoliHolderDAO.Update(Future(policyHolder))
val updatePolicyHolderFailure : Future[PolicyHolderDAO.Failure] = updatePolicyHolderFailureAny.mapTo[PolicyHolderDAO.Failure]
whenReady(updatePolicyHolderFailure, timeout(5 seconds), interval(5 millis)) { failure =>
failure.error.getMessage must be ("When updating a policy holder, we must have an id")
}
}
}
这里是actor的receive方法:
override def receive = {
case PolicyHolderDAO.Update(policyHolder) =>
val s = sender
policyHolder map { p =>
p match {
case _ if p.id.isDefined => s ! update(policyHolder)
case _ => println("HERE"); s ! PolicyHolderDAO.Failure(throw new IllegalArgumentException("When updating a policy holder, we must have an id"), None)
}
}
}
Update的case类如下
case class Update(policyHolder : Future[PolicyHolder]
我要做的是检查保单持有人是否有id。如果它有id,则将其传递给update 方法,如果它没有id,则返回失败。快乐的道路运行良好,而悲伤的道路似乎也运行良好,我的意思是 println("HERE") 被执行。但是,我的测试用例似乎在悲伤的路径场景中没有收到演员的回复。
这是错误信息
[info] A PolicyHolderDAO Actor
HERE
[info] - must return a failure if given a policy holder to update without an id *** FAILED ***
[info] A timeout occurred waiting for a future to complete. Queried 980 times, sleeping 5000000 nanoseconds between each query. (PolicyHolderDAOSystemTest.scala:103)
【问题讨论】: