-1

一个 akka-testkit 问题。

有人可以建议我在收到消息 x 时如何验证演员“A”已回复两条消息 - y 和 z。

消息 x,y,z 都是不同的类型。

我没有看到任何合适的'expect*'函数可以支持这样的测试。

ps 请使用 Scala 中的代码示例。谢谢。

4

1 回答 1

3

其实你可以使用
expectMsgAllClassOf[T](d: Duration, c: Class[_ <: T]*): Seq[T].
完整示例:

case class X(i:Int)  
case class Y(i:Int)  
case class Z(i:Int)

class UnderTest extends Actor {  
 def receive {  
   case x:X =>
     sender ! Y(1)
     sender ! Z(1)
  }
}  

class MyTest extends AkkaTestKit with ImplicitSender {  

val beingTested = system.actorOf(Props[UnderTest])
beingTested ! X(1)

val receivedMsgs = expectedMsgAllClassOf(classOf[Y],classOf[Z])

// Your received messages are in the receivedMsgs sequence first is Y //second is Z
//you can extract them and validating the exact result with assertions  
}
于 2015-11-04T08:24:13.600 回答