Vadim
Vadim

Reputation: 825

expectMsgAnyOf and expectMsgAllOf in Akka testkit typed

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.

Upvotes: 0

Views: 189

Answers (1)

Levi Ramsey
Levi Ramsey

Reputation: 20541

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(",")}")
  }
}

Upvotes: 0

Related Questions