2015-11-03 52 views
-1

akka-testkit问题。验证演员返回的多条消息

有人建议我如何在接收到消息x时验证该角色'A',并回复了两条消息-y和z。

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

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

p.s 请在Scala中的代码示例。 谢谢。

+0

你能提供什么,你已经尝试过 –

+0

A级扩展演员{ 的代码片段高清接收{ 情况下,x:x => <做点事..> 发送! Y() 发件人! Z() .... } } 我使用ImplicitSender特征混入我的测试类,以捕获所有返回的消息。 我可以看到 'expectMsgAllOf [T](d:Duration,obj:T *):Seq [T]'或'expectMsgAllClassOf [T](d:Duration,c:Class [_ <:T] *): Seq [T]' 正在等待相同类型或超类型的消息,但在我的情况下,从A角色返回的消息类型是不同的。 –

回答

2

其实你可以使用
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 
}