单元测试akka因子法

cqoc49vn  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(145)

我有一个演员的形式:

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

这对我不起作用。

xghobddn

xghobddn1#

参与者应该通过你发送给他们的消息来测试,但是如果方法没有访问任何参与者特定的值,比如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 = ???
}

相关问题