英文:
Akka teskit.spawn to return ActorSystem
问题
我正在使用AkkaTestFramework与Akka typed,并且我找不到创建一个我的typed Actor之一的ActorSystem模拟的方法。
我找到了这种方法
val pinger: ActorRefTyped[ItAssetRequest] = testKit.spawn(ItAsset(), "itAssetMock")
但是这个 ActorRefTyped
不包含我在我的类内部需要使用 ask pattern
的 schedule
。
在我接收这个ActorSystem的类中
class RegisterConsumerStream(itAsset: ActorSystemTyped[ItAssetRequest]){
implicit val schedule: typed.Scheduler = itAsset.scheduler
itAsset ? (ref => ItAssetRequest(connectorState, ref))
}
如果我传递一个 ActorRefTyped[ItAssetRequest]
,那么就没有schedule
,所以我不能使用ask模式,因为它需要隐式的schedule
。
有任何想法吗?
英文:
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
得分: 1
你正在使用testKit.spawn
方法创建一个 Actor,而不是一个 ActorSystem。在 Akka TestKit 中,你已经可以在测试中使用一个 ActorSystem:
class AgentSpec extends ScalaTestWithActorTestKit {
val testKitScheduler: Scheduler = system.scheduler
}
使用 testKit 的 spawn 方法,你可以创建一个用于测试行为的 Actor,而不是一个 Actor 系统。
class RegisterConsumerStream(as: ActorSystem[Nothing]){
import akka.actor.typed.scaladsl.AskPattern._
implicit val scheduler = as.scheduler
val ref: ActorRef[ItAssetRequest] = ???
ref.ask(ref => ItAssetRequest())
}
英文:
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())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论