【问题标题】:Extract map from message received by actor从参与者收到的消息中提取地图
【发布时间】:2016-12-13 15:28:13
【问题描述】:

我有一个在消息中接收地图的演员。我想检查该地图的组成部分。

看了这里Pattern matching on testing expected message之后,我这样做了:

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

我认为我没有以正确的方式解决这个问题。

如何从消息中提取地图,然后检查其属性?

【问题讨论】:

    标签: scala akka


    【解决方案1】:

    那个花括号块实际上是一个PartialFunction,你作为第二个参数pf expectMsgPF传递。因此,不需要match

    testReceiver.expectMsgPF() {
          case SomeMessage(answer)  => {
            assert(answer.keys.size == 1)
            assert(answer.keys.head == "appId")
          }
          case _ => Failed
        }
    

    【讨论】:

      【解决方案2】:

      您是否指定了时间限制?参见http://doc.akka.io/docs/akka/current/scala/testing.html中的expectMsgPF方法描述:

      expectMsgPF[T](d: Duration)(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
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-24
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        相关资源
        最近更新 更多