[英]Extract map from message received by actor
我有一個演員會在一條消息中收到一張地圖。 我想檢查該地圖的組成部分。
在此處查看測試預期消息的模式匹配后,我執行了以下操作:
testReceiver.expectMsgPF() match {
case SomeMessage(answer) => {
assert(answer.keys.size == 1)
assert(answer.keys.head == "appId")
}
case _ => Failed
}
但是,我遇到了這個問題:
[error] You can make this conversion explicit by writing `expectMsgPF _` or `expectMsgPF(_,_)(_)` instead of `expectMsgPF`.
[error] testReceiver.expectMsgPF() match {
[error] ^
之后,我將第一行更改為:
testReceiver.expectMsgPF _ match {
之后,在第二行中,我得到:
constructor cannot be instantiated to expected type;
[error] found : my.package.SomeMessage
[error] required: (String, String) => PartialFunction[Any,Nothing] => Nothing
我認為我沒有采用正確的方法。
如何從消息中提取地圖,然后檢查其屬性?
這大括號塊,它實際上是一個PartialFunction
你逝去的PF第二個參數expectMsgPF
。 因此,不需要match
。
testReceiver.expectMsgPF() {
case SomeMessage(answer) => {
assert(answer.keys.size == 1)
assert(answer.keys.head == "appId")
}
case _ => Failed
}
您是否指定了時間限制? 請參閱http://doc.akka.io/docs/akka/current/scala/testing.html中的expectMsgPF
方法說明:
ExpectMsgPF [T](d:持續時間)(pf:PartialFunction [Any,T]):T
在給定的時間段內,必須接收一條消息,並且必須為該消息定義給定的部分功能; 返回將偏函數應用於接收到的消息的結果。 可以不指定持續時間 (在這種情況下, 需要使用空括號), 以改為使用從塊中最內層的截止日期開始 。
嘗試將您的代碼包含在一個within
塊中:
within(1000.millis) {
testReceiver.expectMsgPF() match {
case SomeMessage(answer) => {
assert(answer.keys.size == 1)
assert(answer.keys.head == "appId")
}
case _ => Failed
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.