如何测试演员?

时间:2015-11-03 05:23:53

标签: testing akka

我有一个演员,在其中一个消息上运行此方法:

def addAuctions(actions:List[String]): Unit = {
var i = 0
for(auction <- actions) {
  val x = context.actorOf(Props(new Auction(auction, AUCTION_LIMIT_IN_SECONDS, self)), "auction" + i)
  x ! Auction.Init
  i= i+1
}

}

然后我有以下测试:

"seller" in {
  val seller = system.actorOf(Props[Seller],"seller")
  seller ! Seller.NewAuctions(List("Laptop"))

  // HERE - HOW TO TEST IT ?

}

如何测试卖家发送邮件Auction.Init?

1 个答案:

答案 0 :(得分:1)

关于测试演员的好教程在Akka site

实现所需内容的直接方法是将创建委托actor的代码提取到自己的方法,然后使用测试探针覆盖它。所以你可以按照以下方式做点什么。

class Foo extends Actor {
  // Suppose this is called after receiving a message called DoSomething
  def doSomething(): Unit = delegateActor ! SomeMsg
  def delegateActor: ActorRef = context.actorOf(Props(...))
}

在您的测试代码中,

class TestFoo(dActor: ActorRef) extends Foo {
  override def delegateActor = dActor
}

"test" in {
  val probe = TestProbe()
  val foo = TestActorRef(new TestFoo(probe))
  foo ! DoSomething
  probe.expectMsg(SomeMsg)
}