我有一个形式的演员:
class TestActor(repo: Repo) extends Actor {
implicit val executionContext: ExecutionContext = context.dispatcher
def receive: Receive = {
...
}
def testMethod(stringSeq: Seq[String]): String =
...
}
}
我只想测试方法测试方法。
我正在尝试将测试用例编写为:
class TestActorSpec
extends TestKit(ActorSystem("TestActorSpec"))
with WordSpecLike
with Matchers
with JsonSupport
with MockitoSugar
with BeforeAndAfterAll
with ImplicitSender {
override def afterAll: Unit = TestKit.shutdownActorSystem(system)
implicit val futureDuration: FiniteDuration = 60.seconds
implicit val timeout: Timeout = 10.seconds
val mockedRepo = mock[Repo]
val testActorRef: ActorRef = system.actorOf(
TestActor.props(mockedRepo)
)
"TestActorSpec" should {
"be able to get data " in {
}
}
}
如何从 testActorRef 访问方法测试方法?
我也试过:
TestActorRef[TestActor].underlyingActor.testMethod(Seq("data"))
它对我不起作用。
Actor应该通过你发送给他们的消息来测试他们。但是,如果方法不访问任何特定于参与者的值,例如context
,您仍然可以单独测试方法。
您可以将testMethod
移动到伴随对象并从那里进行测试。
class TestActor(repo: Repo) extends Actor {
implicit val executionContext: ExecutionContext = context.dispatcher
def receive: Receive = {
// call TestActor.testMethod
}
}
object TestActor {
def testMethod(stringSeq: Seq[String]): String = ???
}