akka - 键入 Akka 测试套件中的 expectMsgAnyOf 和 expectMsgAllOf
问题描述
我有 akka testkit(经典)和方法expectMsgAnyOf
,并且expectMsgAllOf
在TestKit
课堂上,让我检查几条消息:
"reply to a greeting" in {
labTestActor ! "greeting"
expectMsgAnyOf("hi", "hello")
}
"reply with favorite tech" in {
labTestActor ! "favoriteTech"
expectMsgAllOf("Scala", "Akka")
}
TestKit
我想用键入的 Akka testkit 重写这些测试,但在和TestProbe
类中找不到这些方法。你能帮我检查消息的顺序和任何消息吗?
解决方案
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(",")}")
}
}
推荐阅读
- cordova - 我们可以使用此 cordova 插件在推送通知中发送图像:用于移动应用程序的 cordova-plugin-fcm 吗?
- abap - 如何在内部表中生成新的自己的列表?abap
- sql-server - SQL REPORTING 服务 (SSRS) 无法启动 - 删除并重新安装
- ios - 后端使用 URLSession.shared.dataTask 为 post 方法接收错误数据
- workflow - 外部用户如何在不创建 APS 帐户的情况下访问 APS(Alfresco 流程服务)?
- ruby-on-rails - 在 Rails 中将多个输入字段作为列表发布,而不是单独的名称。
- react-native - 这可以改进吗?
- java - Android Google 登录在 WebView 中不起作用
- c# - 如何在使用 OLEDB 读取 excel 表时更改数据类型
- android - 用于审查的 Facebook 应用程序