私の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
だったと思いますが、統合のようなテストが必要だとしましょう。