Akka 阿克卡:测试演员收到消息了吗

Akka 阿克卡:测试演员收到消息了吗,akka,scalatest,Akka,Scalatest,我有下一个代码: //TestActor got some message class TestActor extends Actor { def receive = { case string: String => //.... } } //TestReg when create get ActorRef, when i call `pass` method, then should pass text to ActorRef class TestReg(val

我有下一个代码:

//TestActor got some message  
class TestActor extends Actor {

  def receive = {
    case string: String => //.... 
  }
}
//TestReg when create get ActorRef, when i call `pass` method, then should pass text to ActorRef
class TestReg(val actorRef: ActorRef) {
  def pass(text: String) {
   actorRef ! text
  }
}
当我编写测试时:

class TestActorReg extends TestKit(ActorSystem("system")) with ImplicitSender
   with FlatSpecLike with MustMatchers with BeforeAndAfterAll {

   override def afterAll() {
     system.shutdown()
   }

   "actorReg" should "pass text to actorRef" in {
      val probe = TestProbe()

      val testActor = system.actorOf(Props[TestActor])

      probe watch testActor

      val testReg = new TestReg(testActor)
      testReg.pass("test")

      probe.expectMsg("test")
   }
}
我得到一个错误:

java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting for test
如何检查参与者获得的文本?

probe.expectMsg()正在调用probe上的断言。但是您将testActor传递到了TestReg类中

将其更改为下一行,它将工作

val testReg = new TestReg(probe.ref) 
必须调用.ref才能对ActorRef进行探测 您希望在这里执行此操作,而不是在实例化变量时执行此操作,以避免 超出此响应范围的某些bug


我看到的逻辑中的错误是,您认为watch方法让probe看到testactor做了什么。但它的死亡手表不是信息手表。这是不同的。

使用以下内容创建
application.conf
文件:

akka {
  test {
    timefactor = 1.0    
    filter-leeway = 999s
    single-expect-default = 999s
    default-timeout = 999s

    calling-thread-dispatcher {
      type = akka.testkit.CallingThreadDispatcherConfigurator
    }
  }

  actor {
    serializers {
      test-message-serializer = "akka.testkit.TestMessageSerializer"
    }

    serialization-identifiers {
      "akka.testkit.TestMessageSerializer" = 23
    }

    serialization-bindings {
      "akka.testkit.JavaSerializable" = java
    }
  }
}