expectMsgAnyOf和expectMsgAllOf在Akka testkit类型



我有akka testkit(经典)和方法expectMsgAnyOfexpectMsgAllOfTestKit类,这让我检查几个消息:

"reply to a greeting" in {
labTestActor ! "greeting"
expectMsgAnyOf("hi", "hello")
}
"reply with favorite tech" in {
labTestActor ! "favoriteTech"
expectMsgAllOf("Scala", "Akka")
}

我想用Akka testkit类型重写这些测试,但在TestKitTestProbe类中找不到这些方法。你能帮我检查一下留言的顺序和任何留言吗?

您可以在

类型中实现等效的
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(",")}")
}
}

相关内容

  • 没有找到相关文章

最新更新