从actor收到的消息中提取地图

时间:2016-12-13 15:28:14

标签: scala akka

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

在这里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

我认为我没有以正确的方式接近这一点。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您是否指定了时间限制?请参阅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
    }
  }