vendredi 4 novembre 2016

Play! scala and Akka: how to test if an actor A send a message to an actor B (and not an other one)?

I want to test in my Play! application that when an actor A receives a message he sends an other message to an Actor B.

The actor A simply does:

  override def receive: Receive = {
    case i: Long =>
      context
       .actorSelection("/user/b")
       .resolveOne() map(_ ! (i+1))
  }

So I want to test that the message is sent to B. What I have done so far:

import akka.actor.{ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestActorRef, TestKit, TestProbe}
import akka.util.Timeout
import eventsPosts.{EventsPostsActor, SaveEventsPostsActor}
import org.scalatest.{BeforeAndAfterAll, MustMatchers, WordSpecLike}
import testsHelper.Injectors._

import scala.concurrent.duration._

class EventsPostsActorTest
  extends TestKit(ActorSystem("testSystemPosts")) with WordSpecLike with MustMatchers with BeforeAndAfterAll with ImplicitSender {

  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }

  lazy val factory = injector.instanceOf[A.Factory]
  lazy val factoryProps = Props(factory(ec, facebookAPI))

  val actorRefA = TestActorRef[A](factoryProps)

  val actorRefB = TestActorRef[B]
  val probe = TestProbe()

  "A message" must {

    "be send to B" in {
      actorRefA ! 5L
      probe.expectMsg(6L)
    }
  }
}

The exception failed (timeout). I'm likely not using probe as I should (I'm not giving the actor B reference and I fond this strange), but I don't find the correct way to use it.

Aucun commentaire:

Enregistrer un commentaire