我有一些课程:
class MyActor extends Actor{
override def receive: Receive = {
case s: String => doSome()
case i: Int => //something else
}
}
object MyActor{
def doSome() = //some action
}
这是Scala的惯用方式吗?我这样做是为了简化Actor
的测试。我不想创建ActorSystem
并在我的特定情况下编写集成测试。
通常这样做吗?
答案 0 :(得分:2)
不,惯用的方法是将这些方法提取到特征中,并让演员扩展该特征:
trait DoSomething {
def doSome() = {
println("Doing something")
}
}
class MyActor extends Actor with DoSomething {
override def receive: Receive = {
case s: String => doSome()
case i: Int => //something else
}
}