Akka:在集成测试中模拟组件的策略

Akka: strategy to mock a component in an integration test

我需要测试 Akka 中的一个 actor,它是两个 children 中的 parent。在我的集成测试中,我需要模拟这两个 children 之一。 children 都是在构造函数中创建的。我怎样才能得到它?

MyParentActor {
  val childOne = ChildOne.props(...)
  val childTwo = ChildTwo.props(...)
...
}

如果您只需要获取 actor 引用而不创建其父对象,您可以使用 TestActorRef

val actorRef = TestActorRef(new ChildOne(param1, param2))

否则创建父 actor 并使用 context.child(childName) 您可以获得对 childActor 的引用,或者您也可以使用 ActorSelection 通过知道其逻辑路径直接向子 actor 发送消息。