0

I have an actor which receives a map within a message. I would like to check the components of that map.

After looking here Pattern matching on testing expected message, I did this:

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

However, I got this issue:

[error] You can make this conversion explicit by writing `expectMsgPF _` or `expectMsgPF(_,_)(_)` instead of `expectMsgPF`.
[error]     testReceiver.expectMsgPF() match {
[error]                             ^

After that, I changed the first line to:

testReceiver.expectMsgPF _ match {

After that, on the second line I get:

constructor cannot be instantiated to expected type;
[error]  found   : my.package.SomeMessage
[error]  required: (String, String) => PartialFunction[Any,Nothing] => Nothing

I think I'm not approaching this the right way.

How can I extract the map from the message and then check its properties?

Community
  • 1
  • 1
bsky
  • 19,326
  • 49
  • 155
  • 270

2 Answers2

1

That curly-braced block it's actually a PartialFunction you are passing as the second parameter pf expectMsgPF. Therefore, no match is needed.

testReceiver.expectMsgPF() {
      case SomeMessage(answer)  => {
        assert(answer.keys.size == 1)
        assert(answer.keys.head == "appId")
      }
      case _ => Failed
    }
Stefano Bonetti
  • 8,973
  • 1
  • 25
  • 44
1

Have you specified a time limit? See the expectMsgPF method description from http://doc.akka.io/docs/akka/current/scala/testing.html:

expectMsgPF[T](d: Duration)(pf: PartialFunction[Any, T]): T

Within the given time period, a message must be received and the given partial function must be defined for that message; the result from applying the partial function to the received message is returned. The duration may be left unspecified (empty parentheses are required in this case) to use the deadline from the innermost enclosing within block instead.

Try to enclose your code in a within block:

  within(1000.millis) {
    testReceiver.expectMsgPF() match {
      case SomeMessage(answer)  => {
        assert(answer.keys.size == 1)
        assert(answer.keys.head == "appId")
      }
      case _ => Failed
    }
  }
Roman
  • 64,384
  • 92
  • 238
  • 332