Akka测试套件中的`spawn`将返回ActorSystem。

huangapple go评论72阅读模式
英文:

Akka teskit.spawn to return ActorSystem

问题

我正在使用AkkaTestFramework与Akka typed,并且我找不到创建一个我的typed Actor之一的ActorSystem模拟的方法。

我找到了这种方法

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

但是这个 ActorRefTyped 不包含我在我的类内部需要使用 ask patternschedule

在我接收这个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())

}

huangapple
  • 本文由 发表于 2020年10月13日 17:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64332366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定