Typed testkit: Adding proper NoEffects assertions (#25062)
* Adding proper NoEffects assertions * Added NoEffects trait for expectations by type
This commit is contained in:
parent
eef58946d7
commit
7d0eed0631
3 changed files with 29 additions and 4 deletions
|
|
@ -69,13 +69,14 @@ private[akka] final class BehaviorTestKitImpl[T](_path: ActorPath, _initialBehav
|
|||
|
||||
override def expectEffect(expectedEffect: Effect): Unit = {
|
||||
ctx.effectQueue.poll() match {
|
||||
case null ⇒ throw new AssertionError(s"expected: $expectedEffect but no effects were recorded")
|
||||
case null ⇒ assert(expectedEffect == NoEffects, s"expected: $expectedEffect but no effects were recorded")
|
||||
case effect ⇒ assert(expectedEffect == effect, s"expected: $expectedEffect but found $effect")
|
||||
}
|
||||
}
|
||||
|
||||
def expectEffectClass[E <: Effect](effectClass: Class[E]): E = {
|
||||
ctx.effectQueue.poll() match {
|
||||
case null if effectClass.isAssignableFrom(NoEffects.getClass) ⇒ effectClass.cast(NoEffects)
|
||||
case null ⇒ throw new AssertionError(s"expected: effect type ${effectClass.getName} but no effects were recorded")
|
||||
case effect if effectClass.isAssignableFrom(effect.getClass) ⇒ effect.asInstanceOf[E]
|
||||
case other ⇒ throw new AssertionError(s"expected: effect class ${effectClass.getName} but found $other")
|
||||
|
|
@ -84,7 +85,8 @@ private[akka] final class BehaviorTestKitImpl[T](_path: ActorPath, _initialBehav
|
|||
|
||||
def expectEffectPF[R](f: PartialFunction[Effect, R]): R = {
|
||||
ctx.effectQueue.poll() match {
|
||||
case null ⇒ throw new AssertionError(s"expected matching effect but no effects were recorded")
|
||||
case null if f.isDefinedAt(NoEffects) ⇒
|
||||
f.apply(NoEffects)
|
||||
case eff if f.isDefinedAt(eff) ⇒
|
||||
f.apply(eff)
|
||||
case other ⇒
|
||||
|
|
|
|||
|
|
@ -137,5 +137,10 @@ object Effects {
|
|||
/**
|
||||
* Used to represent an empty list of effects - in other words, the behavior didn't do anything observable
|
||||
*/
|
||||
case object NoEffects extends Effect
|
||||
case object NoEffects extends NoEffects
|
||||
|
||||
/**
|
||||
* Used for NoEffects expectations by type
|
||||
*/
|
||||
sealed trait NoEffects extends Effect
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ package akka.testkit.typed.scaladsl
|
|||
import akka.Done
|
||||
import akka.actor.typed.scaladsl.Behaviors
|
||||
import akka.actor.typed.{ ActorRef, Behavior, Props }
|
||||
import akka.testkit.typed.scaladsl.Effects.{ Spawned, SpawnedAdapter, SpawnedAnonymous, SpawnedAnonymousAdapter, Watched, Unwatched }
|
||||
import akka.testkit.typed.scaladsl.Effects.{ NoEffects, Spawned, SpawnedAdapter, SpawnedAnonymous, SpawnedAnonymousAdapter, Unwatched, Watched }
|
||||
import akka.testkit.typed.scaladsl.BehaviorTestKitSpec.{ Child, Father }
|
||||
import akka.testkit.typed.scaladsl.BehaviorTestKitSpec.Father._
|
||||
import org.scalatest.{ Matchers, WordSpec }
|
||||
|
|
@ -114,6 +114,16 @@ class BehaviorTestKitSpec extends WordSpec with Matchers {
|
|||
spawnAnonymous.props should ===(Props.empty)
|
||||
}
|
||||
|
||||
"allow expecting NoEffects by type" in {
|
||||
val testkit = BehaviorTestKit[Father.Command](Father.init)
|
||||
testkit.expectEffectType[NoEffects]
|
||||
}
|
||||
|
||||
"allow expecting NoEffects" in {
|
||||
val testkit = BehaviorTestKit[Father.Command](Father.init)
|
||||
testkit.expectEffect(NoEffects)
|
||||
}
|
||||
|
||||
"return if effects have taken place" in {
|
||||
val testkit = BehaviorTestKit[Father.Command](Father.init)
|
||||
testkit.hasEffects() should ===(false)
|
||||
|
|
@ -140,6 +150,14 @@ class BehaviorTestKitSpec extends WordSpec with Matchers {
|
|||
}
|
||||
childName should ===("child0")
|
||||
}
|
||||
|
||||
"allow assertions using partial functions - match on NoEffect" in {
|
||||
val testkit = BehaviorTestKit[Father.Command](Father.init)
|
||||
val hasEffects = testkit.expectEffectPF {
|
||||
case NoEffects ⇒ false
|
||||
}
|
||||
hasEffects should ===(false)
|
||||
}
|
||||
}
|
||||
|
||||
"BehaviorTestkit's spawn" must {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue