阿卡:测试演员得到了消息

时间:2013-11-16 07:50:26

标签: 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 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

如何查看演员获得文字的内容?

2 个答案:

答案 0 :(得分:6)

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

将其更改为以下行,它将起作用

val testReg = new TestReg(probe.ref) 

必须调用.ref才能将探测器调用到ActorRef中 而你想在这里做,而不是在变量的实例化中避免 某些超出此回复范围的错误

我认为逻辑中的错误是你认为watch方法让探针看到测试演员的作用。但它的死亡守望不是留言。这是不同的。

答案 1 :(得分:1)

使用以下方法创建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
    }
  }
}