Akka teskit.spawn to return ActorSystem

168 views Asked by At

I'm using AkkaTestFramework with Akka typed, and I cannot find a way to create an ActorSystem mock for one of my typed Actor.

I found this way

val pinger: ActorRefTyped[ItAssetRequest] = testKit.spawn(ItAsset(), "itAssetMock")

But this ActorRefTyped does not contain a schedule which I need internally in my class to use ask pattern

In my class where I receive this ActorSystem

class RegisterConsumerStream(itAsset: ActorSystemTyped[ItAssetRequest]){


  implicit val schedule: typed.Scheduler = itAsset.scheduler


  itAsset ? (ref => ItAssetRequest(connectorState, ref)


}

If I pass a ActorRefTyped[ItAssetRequest] there's no schedule so I cannot use the ask pattern since it needs the schedule implicit.

Any idea?

1

There are 1 answers

14
Emiliano Martinez On

You are creating an Actor with testKit.spawn method not an ActorSystem. With the Akka TestKit you already have available an ActorSystem inside your tests:

class AgentSpec extends ScalaTestWithActorTestKit { 

  val testKitScheduler : Scheduler = system.scheduler 

}

With the spawn method from the testKit you create an actor for testing your behaviors not an actor system.

class RegisterConsumerStream(as: ActorSystem[Nothing]){

  import akka.actor.typed.scaladsl.AskPattern._
  implicit val scheduler = as.scheduler

  val ref: ActorRef[ItAssetRequest] = ???
  ref.ask(ref => ItAssetRequest())

}