繁体   English   中英

从演员收到的消息中提取地图

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM