liveBook · Manning

Effect

As explained in Akka in Action, Second Edition

An Akka Typed test effect is a recorded representation of an actor-context action such as spawning, stopping, or scheduling.

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

How Akka in Action, Second Edition covers it

How deep it goes
Explains queue-backed assertions for NoEffects and Spawned, while noting roughly a dozen effect types without enumerating them.
Examples
Uses a manager that creates an ignoring string worker after receiving CreateChild("adan").
What it assumes
Assumes typed actor behaviors and messages while demonstrating the test-kit interaction from construction through assertion.
What you can do after
Apply it via BehaviorTestKit to assert that a command records an expected actor-context action.
How it connects
Connects BehaviorTestKit, ActorContext, Behaviors.ignore, Behavior.same, spawning, and event sourcing's Effect.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.