mike
mike

Reputation: 859

Testing actor system

i have a next code

class MySystem(outerResourse: OuterResourse) extends Actor {

  val firstActor = context.actorOf(Props(new FirstActor(outerResourse)), "first")

  val secondActor = context.actorOf(Props(new SecondActor(firstActor)), "second")

  secondActor ! Go

  def receive = {
    case x: AnotherMessage => printl(s"another message: $x")
    case x => println(x)  
  }
}


class FirstActor(outerResourse: OuterResourse) extends Actor {
  def receive = {
   case Test => 
     context.parent ! AnotherMessage 
     sender ! "ok"
  }
}

class SecondActor(firstActor: ActorRef) extends Actor {
  def receive = {
    case Go => firstActor ! Test
    case "ok" => println("ok")
  }
}

Here OuterResourse is a any resourse - file, internet connection...

i would like to check a behavior, but i in embarrassment, i do not know how to check that second actor will be got a "ok", and mySystem actor will be got a AnotherMessage

class MyTest(_system: ActorSystem) extends TestKit(_system)
  with ImplicitSender with FunSpecLike with Matchers {

  def this() = this(ActorSystem("myTest"))

  val outerResourse = new OuterResourse()
  val mySystem = system.actorOf(Props(new MySytem(outerResourse)))

  describe("Actors") {
    it("should get AnotherMessage message and ok message") {
      ???
    }
  }
}

Hot to check, that a secondActor got a "ok" message?

Upvotes: 0

Views: 152

Answers (1)

Koziołek
Koziołek

Reputation: 2874

You could set custom OutputStream for println usnig Console.withOut. It could be mock and "wait" for OK message from first actor. But it is not very nice....

// edit: please read documentation http://doc.akka.io/docs/akka/snapshot/scala/testing.html Akka provide they owm testing "framework"

Upvotes: 1

Related Questions