An Akka Typed test effect is a recorded actor-context action. Rather than observing an asynchronously running actor, `BehaviorTestKit` supplies a stubbed context and records actions in a queue, making assertions deterministic. Effects include spawning and the absence of an action; other actor-context operations have their own effect representations. section 4.2.1
Test an effect by creating `BehaviorTestKit` with the behavior under test, sending a command with `run`, and polling the recorded actions with `expectEffect`. This separates a command's observable context action from the actor's returned behavior. section 4.2.1
Assert worker creation
A manager receives `CreateChild("adan")` and calls `context.spawn` for a worker. Before the command, assert `NoEffects`; afterward, assert `Spawned(SimplifiedWorker(), "adan")`. The test verifies that spawning occurred without starting a real child actor. section 4.2.1
Listing 4.1 Simplified manager testing spawning
import akka.actor.testkit.typed.scaladsl.BehaviorTestKit
import akka.actor.testkit.typed.Effect.{ NoEffects, Scheduled, Spawned }
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
class SyncTestingSpec extends AnyWordSpec with Matchers {
"Typed actor synchronous testing" must {
"spawning takes place" in {
val testKit = BehaviorTestKit(SimplifiedManager())
testKit.expectEffect(NoEffects) #1
testKit.run(SimplifiedManager.CreateChild("adan")) #2
testKit.expectEffect(Spawned(SimplifiedWorker(), "adan")) #3
}
}
}
The listing shows the complete synchronous test: construct the kit, expect no recorded action, run the command, and expect the matching spawn effect. section 4.2.1
`Effect` also names the result produced by an event-sourced command handler, but this is a different use from a recorded test effect. A persistence command handler selects reactions such as `persist`, `reply`, or `stop`; after successful persistence, the event handler updates state. A `thenRun` side effect can accompany persistence, while event handlers should avoid side effects because events are replayed during recovery. section 9.2.1
