我有一个阿卡演员:

class MyActor extends Actor {
  def recieve { ... }

  def getCount(id: String): Int = {
    //do a lot of stuff
    proccess(id)
    //do more stuff and return
  }
}


我正在尝试为getCount方法创建一个单元测试:

it should "count" in {
    val system = ActorSystem("Test")
    val myActor = system.actorOf(Props(classOf[MyActor]), "MyActor")

    myActor.asInstanceOf[MyActor].getCount("1522021") should be >= (28000)
}


但它不起作用:

  java.lang.ClassCastException: akka.actor.RepointableActorRef cannot be cast to com.playax.MyActor


我该如何测试这种方法?

最佳答案

做这样的事情:

import org.scalatest._
import akka.actor.ActorSystem
import akka.testkit.TestActorRef
import akka.testkit.TestKit

class YourTestClassTest extends TestKit(ActorSystem("Testsystem")) with FlatSpecLike with Matchers {

  it should "count plays" in {
    val actorRef = TestActorRef(new MyActor)
    val actor = actorRef.underlyingActor
    actor.getCount("1522021") should be >= (28000)
  }
}

08-18 15:27