在测试Akka演员时,如何验证发送给自己的消息?

Eri*_*ert 12 testing scala akka

我有一个类似于以下Actor中的Actor.

case class SupervisingActor() extends Actor {

    protected val processRouter = //round robin router to remote workers
    override def receive = {
        case StartProcessing => { //sent from main or someplace else
            for (some specified number of process actions ){
                processRouter ! WorkInstructions
            }
        }
        case ProcessResults(resultDetails) => {  //sent from the remote workers when they complete their work
            //do something with the results
            if(all of the results have been received){
                //*********************
                self ! EndProcess  //This is the line in question
                //*********************
            }
        }
        case EndProcess {
            //do some reporting
            //shutdown the ActorSystem
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

如何在测试中验证EndProcess消息是否已发送给self?

我正在使用scalatest 2.0.M4,Akka 2.0.3和Scala 1.9.2.

Rol*_*uhn 12

发送给自己的演员非常关注该演员如何执行特定功能的详细信息,因此我宁愿测试该消息的效果,而不是该消息是否已被传递.我认为发送给self与在经典OOP中对象上使用私有帮助器方法相同:你也不测试是否调用了那个,你测试最后是否发生了正确的事情.

作为旁注:您可以实现自己的消息队列类型(请参阅https://doc.akka.io/docs/akka/snapshot/mailboxes.html#creating-your-own-mailbox-type)并允许检查或跟踪消息发送.这种方法的优点在于它可以纯粹通过配置插入到测试中的actor中.

  • 听听库恩 (3认同)