在我的 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
单独测试,但是假设我想要那种类似集成的测试。