1

我正在测试Actor我正在处理的新消息如何处理意外消息。我想断言它GibberishException在这些情况下会抛出 a 。这是到目前为止的测试和实现:

测试:

"""throw a GibberishException for unrecognized messages""" in {
  //define a service that creates gibberish-speaking repositories
  val stubs = new svcStub(
    actorOf(new Actor{
      def receive = { case _ => {
          self.channel ! "you're savage with the cabbage"
        }
      }
    })
  )
  val model = actorOf(new HomeModel(stubs.svc,stubs.store))
  val supervisor = Supervisor(
    SupervisorConfig(
      OneForOneStrategy(List(classOf[Exception]), 3, 1000),
      Supervise(model,Permanent) :: Nil
    )
  )
  try{
    intercept[GibberishException] {
      supervisor.start
      model !! "plan"
    }
  } finally {
    supervisor.shutdown
  }
  stubs.store.plan should equal (null)
  stubs.svcIsOpen should be (false)
}

执行:

class HomeModel(service: PlanService, store: HomeStore)
extends Actor {
  private val loaderRepo = service.getRepo()
  private var view: Channel[Any] = null

  override def postStop() = {
    service.close()
  }

  def receive = {
    case "plan" => {
      view=self.channel
      loaderRepo ! LoadRequest()
    }
    case p: Plan => {
      store.plan=p
      view ! store.plan
    }
    case _ => throw new GibberishException(_)
  }
}

但是,当我运行测试时,异常详细信息会到达Supervisor我建立的位置,但我不知道如何对它们做任何事情(比如记录它们或测试它们的类型)。我希望能够从主管那里获得异常详细信息,以便我可以在我的测试中重新抛出和拦截它们。在测试方法之外,如果您想在正在运行的应用程序的 UI 中报告异常的性质,我可以想象这很有用。有没有办法从Supervisor它发生的时候得到它?

4

1 回答 1

3

将 OneForOneStrategy 更改为仅处理 GibberishException,应该可以解决它。

于 2011-08-07T18:30:30.323 回答