0

在我的 TestKit 测试中

"A History Actor" must {

    // given
    val historyActorRef = TestActorRef(new HistoryActor("history-file.log"))       // Creation of the TestActorRef
    val writerActorRef = TestActorRef(new WriterActor("history-file.log"))       // Creation of the TestActorRef

    historyActorRef.underlyingActor.writerActor = writerActorRef

    "receive messages and change state" in {  // integration-like test

      // This call is synchronous. The actor receive() method will be called in the current thread

      // when
      historyActorRef ! WriteMsg("line 1")

      // then (1) - got WriteResult (from WriterActor as result of getting WriteMsg)
      within(200 millis) {
        expectMsg(WriteResult(1, 7))
      }

      // then (2) - state
      historyActorRef.underlyingActor.lastWrite must equal(WriteResult(1,7)) // With actorRef.underlyingActor, we can access the react actor instance created by Akka
    }
  }

这个测试失败了,因为它仍然是WriteResult(0,0)

我的工作方式HistoryActor

   case cmd: WriteMsg => {

      log.info("forwarding " + cmd + " to the writer" )

      writerActor ! cmd

    }

    case result: WriteResult => {

      log.info("WriteResult: " + result)

      lastWrite = result                  // update the state

    } 

那么,当我们检查结果时,如何进行测试以确保WriteResult已经处理呢?

PS我想我应该考虑WriterActor单独测试,但是假设我想要那种类似集成的测试。

4

1 回答 1

1

TestActorRef如果您还没有这样做,您需要在 s 中将两个正在测试的参与者包装起来。这将确保两者都使用调用线程调度程序来消除测试场景的异步性。您还可以考虑用writeActora替换您TestProbe的测试,然后模拟它的行为。

更新

根据我的评论:

您所说expectMsg的是测试本身(由 引入的隐式发送者TestKit)正在从最初发送的消息中接收响应消息以启动测试。我认为这不是您要断言的内容,因此我建议您将其删除。我认为您是想说写演员收到了该消息,但这不是测试套件断言的工作方式。只需验证内部状态是否已更新就足够了

于 2015-07-08T16:41:48.810 回答