我有一个演员的形式:
class TestActor(repo: Repo) extends Actor {
implicit val executionContext: ExecutionContext = context.dispatcher
def receive: Receive = {
...
}
def testMethod(stringSeq: Seq[String]): String =
...
}
}
我只想测试方法testMethod。
我尝试将测试用例编写为:
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访问方法testMethod?
我也试探着:
TestActorRef[TestActor].underlyingActor.testMethod(Seq("data"))
这对我不起作用。
1条答案
按热度按时间xghobddn1#
参与者应该通过你发送给他们的消息来测试,但是如果方法没有访问任何参与者特定的值,比如
context
,你仍然可以单独测试方法。您可以将
testMethod
移到伴随对象中,并从那里测试它。