BehaviorTestKit.expectEffect(Spawned) seems useless for behaviors created by methods

akka-typed

(Heiko Seeberger) #1
object Test {

  def main(args: Array[String]): Unit = {

    def child(s: String): Behavior[String] =
      Behaviors.immutable {
        case (_, `s`) => Behaviors.same
        case (_, _)   => Behaviors.stopped
      }

    sealed trait Command

    val parent: Behavior[Command] =
      Behaviors.setup { context =>
        context.spawn(child("foo"), "child")
        Behaviors.empty
      }

    val testKit = BehaviorTestKit(parent)
    testKit.expectEffect(Effects.Spawned(child("foo"), "child"))
  }
}

this leads to

Exception in thread "main" java.lang.AssertionError: assertion failed: expected: Spawned(Immutable(MainTests.scala:34-36),child,EmptyProps) but found Spawned(Immutable(MainTests.scala:34-36),child,EmptyProps)

So it seems that the two behaviors are compared using equals and this doesn’t work.


(Patrik Nordwall) #2

There will be a new expectEffectType[T] that can be used for this. E.g.

testKit.expectEffect[Spawned].childName should === ("child")

See PR https://github.com/akka/akka/pull/24730


(Heiko Seeberger) #3

Great! I have also added an expectEffectPF to my project.

  implicit final class TestKitOps[A](val testKit: BehaviorTestKit[A]) extends AnyVal {

    def expectEffectPF[B](pf: PartialFunction[Effect, B]): B = {
      def unexpected(effect: Effect) = throw new AssertionError(s"Unexpected Effect: $effect")
      pf.applyOrElse(testKit.retrieveEffect(), unexpected)
    }
  }