为什么 Future 没有从 actor 返回到 ScalaTest 的 whenReady?

Why is Future not returned from actor to ScalaTest's whenReady?

我有一个 Future 的问题,它不是使用 whenReady 从 ScalaTest 案例中的参与者 return 编辑的。

这里是测试用例

"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")
    }
  }
}

演员的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案例class如下

case class Update(policyHolder : Future[PolicyHolder]

我想做的是检查保单持有人是否有id。如果它有 id,则将其传递给 update 方法,如果它没有 id,则 return 失败。快乐的路径工作正常,悲伤的路径似乎也工作正常,我的意思是 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)
PolicyHolderDAO.Failure(throw new IllegalArgumentException("When updating a policy holder, we must have an id"), None)

意味着您将抛出一个 IllegalArgumentException,因此不会构造任何消息,并且错误将被发送到 actor supervisor。

您的意思是创建异常并return它在消息中,对吗?

PolicyHolderDAO.Failure(new IllegalArgumentException("When updating a policy holder, we must have an id"), None)