-1

An akka-testkit question.

Could some one advise on how do I validate that actor 'A' when received message x, has responded with two messages - y and z.

Messages x,y,z are all of different types.

I don't see any suitable 'expect*' function that would support such tests.

p.s Code examples in Scala please. Thanks.

Eli Golin
  • 373
  • 1
  • 16
  • You could provide a code snippet of what you already tried – Onilton Maciel Nov 03 '15 at 15:56
  • class A extends Actor { def receive { case x:X => sender ! Y() sender ! Z() .... } } I'm using the ImplicitSender trait mixed into my test class, in order to catch all returned messages. I can see that `expectMsgAllOf[T](d: Duration, obj: T*): Seq[T]` or `expectMsgAllClassOf[T](d: Duration, c: Class[_ <: T]*): Seq[T]` are expecting messages of the same type or super type, but in my case the types of messages which are returned from actor A are different. – Eli Golin Nov 04 '15 at 07:12

1 Answers1

3

Actually you can use the
expectMsgAllClassOf[T](d: Duration, c: Class[_ <: T]*): Seq[T].
Full example:

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  
}
Eli Golin
  • 373
  • 1
  • 16