【问题标题】:akka Actor unit testing using testkit使用 testkit 的 akka Actor 单元测试
【发布时间】:2015-06-03 00:21:32
【问题描述】:

many在被测试的Actor响应询问时使用akka-testkit的例子:

//below code was copied from example link
val actorRef = TestActorRef(new MyActor)
// hypothetical message stimulating a '42' answer
val future = actorRef ? Say42
val Success(result: Int) = future.value.get
result must be(42)

但是我有一个不响应发送者的 Actor;而是将消息发送给单独的参与者。一个简化的例子是:

class PassThroughActor(sink : ActorRef) {
  def receive : Receive = {
    case _ => sink ! 42
  }
}

TestKit 有一套 expectMsg 方法,但我找不到任何创建测试接收器 Actor 的示例,该 Actor 可能会在单元测试中收到消息。

是否可以测试我的PassThroughActor

提前感谢您的考虑和回复。

【问题讨论】:

  • 您可以创建探针和探针演员。请继续阅读文档。
  • 啊,谢谢。显然是一个足够普遍的问题。不过很难用谷歌搜索,因此提出了这个问题。如果您提交答案,我会投票...

标签: scala unit-testing akka scalatest


【解决方案1】:

如 cmets 中所述,您可以使用 TestProbe 来解决此问题:

val sink = TestProbe()
val actorRef = TestActorRef(Props(new PassThroughActor(sink.ref)))

actorRef ! "message"

sink.expectMsg(42)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 2015-10-25
    • 2018-03-10
    • 2020-10-04
    • 2013-05-23
    • 2017-10-13
    • 1970-01-01
    • 2019-02-12
    相关资源
    最近更新 更多