'How to use BehaviorTestKit to verify the right child was spawned

I'm trying to test determine that my actor spawned a child with the right parameters.

There is

testKit.expectEffect(Spawned(behavior,name,props))

But it's impossible to match since any behavior created inside your spawning behavior will never match a copy you create since behaviors only provide reference based equality.

Alternatively there is

testKit.expectEffectPF()

This let's you can at capture the behavior, but once again the reference is entirely opaque to inspection.

Short of providing some child builder function for the behavior under test that can then be mocked and inspected for its call signature, there doesn't seem to be a way to do this with the effect tooling provided by BehaviorTestKit.



Solution 1:[1]

Try using expectEffectType(Spawned[T])

Solution 2:[2]

In order to get the ActorRef to the Spawned child one doesn't seem to be able to use expectedEffected instead one need to use expectEffectType

Say you want to test an Actor that spawns a child and then watches it, it would be something like the below

    val childActorDef = Behaviors.receiveMessage[Any] { _ =>
      Behaviors.same
    }
    val childId                                        = "ChildActor"
    
    val testKit: BehaviorTestKit[StartStopper.Command] = BehaviorTestKit(ActorUnderTest.actorDef)    

    testKit.run(ActorUnderTest.StartChildAndWatch(childActorDef, childId))
    val childSpawn : Spawned[Any] = testKit.expectEffectType[Spawned[Any]]
    val childRef : ActorRef[Any] = childSpawn.ref
    testKit.expectEffect(Watched[Any](childRef.ref))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 irigoy22
Solution 2 David Lilljegren