In my TestKit test
"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
}
}
This test fails, because it is still WriteResult(0,0)
The way in works in my 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
}
So, how to make test to make sure that WriteResult
has already handled when we check for result?
P.S.
I guess I should have considered to test WriterActor
separately, but let's say I want that integration-like test.