我遇到了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 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)
答案 0 :(得分:4)
PolicyHolderDAO.Failure(throw new IllegalArgumentException("When updating a policy holder, we must have an id"), None)
意味着您将抛出IllegalArgumentException,因此不会构造任何消息,并且错误将被发送给actor主管。
你的意思是创建异常并将其返回到消息中,对吗?
PolicyHolderDAO.Failure(new IllegalArgumentException("When updating a policy holder, we must have an id"), None)