簡體   English   中英

使用testkit進行akka Actor單元測試

[英]akka Actor unit testing using testkit

當被測試的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嗎?

預先感謝您的考慮和回應。

如評論中所述,您可以使用TestProbe解決此問題:

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

actorRef ! "message"

sink.expectMsg(42)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM