gpt4 book ai didi

scala - 如何在 akka actor 中测试公共(public)方法?

转载 作者:行者123 更新时间:2023-11-28 19:54:51 25 4
gpt4 key购买 nike

我有一个 akka Actor :

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)
}
}

关于scala - 如何在 akka actor 中测试公共(public)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22648546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com