Scala 通过system.actorOf获得的ActorRef是否等于此参与者内的self?

Scala 通过system.actorOf获得的ActorRef是否等于此参与者内的self?,scala,akka,akka-testkit,Scala,Akka,Akka Testkit,我设计了一个演员,应该在开始前将其“actorRef”发送给另一个演员: class MyActor(notifier: ActorRef) extends Actor { override def preStart(): Unit = { notifier ! Register(self) } ... } case class Register(actor: ActorRef) 然后我为这个演员写了一份说明书: class MyActorSpec extends Tes

我设计了一个演员,应该在开始前将其“actorRef”发送给另一个演员:

class MyActor(notifier: ActorRef) extends Actor {
  override def preStart(): Unit = {
    notifier ! Register(self)
  }
  ...
}

case class Register(actor: ActorRef)
然后我为这个演员写了一份说明书:

class MyActorSpec extends TestKit(ActorSystem("MyActorSpec"))
                  with ImplicitSender 
                  with WordSpecLike 
                  with Matchers 
                  with BeforeAndAfterAll {

  "MyActor" should {
     val notifier = TestProbe()
     "register itself in notifier" in {
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         notifier.expectMsg(Register(myActor))
     }
  }
}
当我运行测试时,它会失败,并显示以下消息:
assertion failed:expected Register(Actor)[akka://MyActorSpec/user/$b#1849780829]),找到寄存器(Actor[akka://MyActorSpec/user/$a#1143150267])


因此,在MyActor中通过
self
获得的ActorRef似乎不等于在我的测试中通过
system.actorOf
获得的ActorRef。有什么建议吗?

以下代码对我来说运行良好(测试通过):


理想情况下,您发布的代码甚至不应在以下位置编译:

val myActor = system.actorOf(Props(classOf[MyActor], notifier))
因为构造函数需要一个我们没有传递的
ActorRef
。但是纠正它,它是有效的:

class MyActorSpec extends TestKit(ActorSystem("MyActorSpec"))
with ImplicitSender
with WordSpecLike
with Matchers
with BeforeAndAfterAll {

  "MyActor" should {
    val notifier = TestProbe()
    "register itself in notifier" in {
      val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
      notifier.expectMsg(Register(myActor))
    }
  }
}

首先,为了确保不会因为
ActorProbe
而发生特殊的魔法,我在下面编写了一个简单的老演员类

object Prac {
  def main(args: Array[String]) {
    val system = ActorSystem("HelloSystem")
    val myActor = system.actorOf(Props(classOf[MainActor]))
  }

}

class MyActor(notifier: ActorRef) extends Actor {

  override def preStart(): Unit = {
    notifier ! Register(self)
  }

  override def receive: Receive = {
    case x => println("My Actor ->"+x)
  }
}

case class Register(actor: ActorRef)

class MainActor extends Actor{

  val actor = context.actorOf(Props(classOf[MyActor], self))

  override def receive = {
    case Register(x) =>
      println(actor == x)
      context.system.shutdown()
  }
}

并打印
true
。所以你的节目没有问题。

我已经弄明白了。这是因为我在几个测试用例中使用了SharedTestProbe,在这些测试用例中我创建了MyActor的不同实例

class MyActorSpec extends TestKit(ActorSystem("MyActorSpec"))
                  with ImplicitSender 
                  with WordSpecLike 
                  with Matchers 
                  with BeforeAndAfterAll {

  "MyActor" should {
     val notifier = TestProbe()
     "register itself in notifier" in {
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         notifier.expectMsg(Register(myActor))
     }
     "do some useful work" in {
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         ....
     }
  }
}
相反,为每个测试用例使用TestProbe的新实例会有所帮助

class MyActorSpec extends TestKit(ActorSystem("MyActorSpec"))
                  with ImplicitSender 
                  with WordSpecLike 
                  with Matchers 
                  with BeforeAndAfterAll {

  "MyActor" should {
     "register itself in notifier" in {
         val notifier = TestProbe()
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         notifier.expectMsg(Register(myActor))
     }
     "do some useful work" in {
         val notifier = TestProbe()
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         ....
     }
  }
}

无论如何,感谢大家证明,对于单个测试用例,它工作得很好。

是的,我的问题中实际上有notifier.ref,我已经纠正了它。
class MyActorSpec extends TestKit(ActorSystem("MyActorSpec"))
                  with ImplicitSender 
                  with WordSpecLike 
                  with Matchers 
                  with BeforeAndAfterAll {

  "MyActor" should {
     "register itself in notifier" in {
         val notifier = TestProbe()
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         notifier.expectMsg(Register(myActor))
     }
     "do some useful work" in {
         val notifier = TestProbe()
         val myActor = system.actorOf(Props(classOf[MyActor], notifier.ref))
         ....
     }
  }
}