0

I have akka testkit (classic) and methods expectMsgAnyOf and expectMsgAllOf in TestKit class, which let me check several messages:

    "reply to a greeting" in {
     labTestActor ! "greeting"
     expectMsgAnyOf("hi", "hello")
   }

   "reply with favorite tech" in {
     labTestActor ! "favoriteTech"
     expectMsgAllOf("Scala", "Akka")
   }

I want to rewrite these tests with Akka testkit typed but can't find these methods in TestKit and TestProbe classes. Could u help me to check the sequence of messages and any message.

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
Vadim
  • 753
  • 8
  • 22

1 Answers1

0

You could implement equivalents in typed as

def expectMsgAnyOf[T](probe: TestProbe[T])(candidates: T*): Unit = {
  val c = candidates.toSet
  val nextMsg = probe.receiveMessage()
  if (!c(nextMsg)) {
    throw new AssertionError(s"Expected one of $c, got $nextMsg")
  }
}

def expectMsgAllOf[T](probe: TestProbe[T])(expected: T*): Unit = {
  import scala.collection.mutable.Buffer

  val e = Buffer(expected: _*)
  val nextMsgs = probe.receiveMessages(candidates.size)
  nextMsgs.foreach { msg =>
    val idx = e.indexOf(msg)
    if (idx == -1) {
      throw new AssertionError(s"Received unexpected message: $msg")
    }
    e.remove(idx)
  }

  if (e.nonEmpty) {
    throw new AssertionError(s"Expected messages not received: ${e.mkString(",")}")
  }
}
Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30