Akka TestKit测试多个Actor消息响应

时间:2017-05-30 15:29:29

标签: akka

我有一个接收消息的Actor,并且在获得该消息后立即变为上下文。在那之后我启动了一个Observable,在那个Observable中,我一直向这个Actor发送一些特定的消息,直到我达到一个理想的状态。然后我开始编写单元测试来测试这个Actor行为,但我想知道如何检查期望。所以这是我的测试演员:

"start to RampUp when a Dispatch command is sent" in {
  rampUpTypeSimActor ! Dispatch(rampUpTypeCfg.maxPower)
  expectMsgPF(12.seconds) {
    case state: PowerPlantState =>
      // check the signals
      assert(
        state.signals(activePowerSignalKey).toDouble === 800.0,
        "expecting activePower to be 800.0, but was not the case"
      )
      assert(
        state.signals(isDispatchedSignalKey).toBoolean,
        "expected isDispatched signal to be true, but was false instead"
      )
      assert(
        state.signals(isAvailableSignalKey).toBoolean,
        "expected isAvailable signal to be true, but was false instead"
      )
      assert(
        state.rampRate === initPowerPlantState.rampRate,
        "rampRate did not match"
      )
      assert(
        state.setPoint === initPowerPlantState.setPoint,
        "setPoint did not match"
      )
    case x: Any => // If I get any other message, I fail
      fail(s"Expected a PowerPlantState as message response from the Actor, but the response was $x")
  }
}

发送Dispatch命令后,如何等待15秒?我需要发送另一个名为Status的命令,以便我可以检查我的期望,但是我想等待15秒然后将状态消息发送给我的演员!

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方式。我不得不将Dispatch(...)命令移动到内部块中:

  within(12.seconds) {
    rampUpTypeSimActor ! Dispatch(rampUpTypeCfg.maxPower)
    expectNoMsg
  }

然后向Actor发送状态消息以检查期望!