簡體   English   中英

嘲笑阿卡的兒童演員

[英]Mocking child actor in Akka

我正在嘗試為我的演員編寫單元測試,並堅持基本的模擬。 PriceAggregateActor正在使用akka持久性,我不想為它傳遞所有的conf並且想要完全嘲笑它。

這是我要測試的演員

object CommandPriceActor {
  def apply() = Props(classOf[CommandPriceActor], PriceAggregateActor())
}

class CommandPriceActor(priceAggregateActorProps: Props) extends Actor with ActorLogging {

  val priceAggregateActor = context.actorOf(priceAggregateActorProps, "priceAggregateActor")

所以在我的測試中,我正在嘗試做類似的事情:

class CommandPriceActorTest extends TestKit(ActorSystem("test-benefits",
  ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"] """))) with FlatSpecLike with Matchers
  with BeforeAndAfterAll with Eventually{

  class MockedChild extends Actor {
    def receive = {
      case _ => lala
    }
  }

  val probe = TestProbe()
  val commandPriceActor = TestActorRef(new CommandPriceActor(Props[MockedChild]))

我總是得到:

Caused by: java.lang.IllegalArgumentException: no matching constructor found on class CommandPriceActorTest$MockedChild for arguments []

為什么抱怨mockedChild? 它不應該采用任何構造函數參數。

這是因為MockedChild是您測試的兒童演員。 缺少的構造函數參數是對測試的引用(它是父類)。

你有三個選擇:

  1. this的引用傳遞給Props
  2. 使用Props的命名參數形式
  3. 使MockedChild成為頂級類(或對象的成員)

選項1

val probe = TestProbe()
val mockProps = Props(classOf[MockedChild], this)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))

選項2

val probe = TestProbe()
val mockProps = Props(new MockedChild)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))

選項3

val probe = TestProbe()
val mockProps = Props(new CommandPriceActorTest.MockedChild)
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps))

// ....

object CommandPriceActorTest {
  class MockedChild extends Actor {
    def receive = {
      case _ => lala
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM