MassTransit's Test Harness can verify saga state machines without a broker. This walkthrough builds an order-processing saga and tests its correlation, state transitions, published events and failure paths inside an ASP.NET Core solution.
Key points
- The Test Harness runs on an in-memory transport, so saga tests need no RabbitMQ or Azure Service Bus broker.
- AddMassTransitTestHarness registers the state machine, and InMemoryRepository keeps saga instances without a database.
- Consumed, Published, Sent, Created, Events and StateChanges collections show what the bus and the saga actually did.
- GetSagaStateMachineHarness() plus ContainsInState() verify the current state and the business data stored on the instance.
- CorrelateById() binds each event to a CorrelationId, so concurrent orders must update their own saga instance.
- MassTransit.TestFramework should match the application's MassTransit version, because harness APIs differ between releases.
Why it matters
Saga bugs rarely show up in a happy-path demo; they surface as a duplicate charge or an order stuck in Submitted weeks later. Moving those checks into the Test Harness puts state transitions, correlation and published events under the same `dotnet test` run as the rest of the code, so a misconfigured During() block fails the build instead of production.
Before relying on it, keep MassTransit.TestFramework and MassTransit on matching versions, and remember the in-memory repository proves workflow logic, not persistence or transport behaviour — the EF Core or database-backed repository and the real broker still need at least one integration test.
Real-world use
- An order-processing workflow that validates, reserves inventory, charges payment and arranges delivery across separate services.
- Regression tests for duplicate or late messages that could push an order into the wrong state.
- Compensation checks where an OrderRejected event must publish a cancellation and stop the workflow.
- CI pipelines that want saga coverage on every commit without provisioning a broker.
This summary was generated by the agent from the original post on C# Corner. The full text and all rights belong to its author and publisher. Go to original