2017-05-30 76 views
1

我有一個Actor接收消息,並在獲取該消息後立即生成上下文。之後,我旋轉了一個Observable,在那個Observable中,我一直給這個Actor發送一些特定的消息,直到達到所需的狀態。然後我開始編寫單元測試來測試這個Actor行爲,但是我想知道如何檢查期望值。所以這裏是我的測試演員:Akka TestKit測試多個演員消息響應

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

如何等待15秒後發送調度命令?我需要發送另一個名爲狀態的命令,以便我可以檢查我的期望,但是我想等待15秒,然後將狀態消息發送給我的演員!

有什麼建議嗎?

回答

0

這就是我解決它的方法。我不得不調度(...)命令進入一個塊中:

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

,然後發送一個狀態消息給演員以檢查期望!