Test a routing slip activity
Testing routing slip activities is crucial because they encapsulate specific steps in a larger business process. Unlike traditional consumers that might just save data, activities often interact with external services, make decisions, and carry state (variables) forward to later activities. Verifying that an activity correctly handles arguments, executes its logic, and produces the expected completed or faulted events ensures the integrity of the entire distributed transaction.
Testing a routing slip activity is different from testing a consumer. A routing slip activity is a class that implements the IActivity<TArguments, TLog> interface. The Execute method is called when the activity is executed.
Create the Unit Test
Section titled “Create the Unit Test”To test a routing slip activity, create a unit test using the test harness, add the activity to the test harness, and execute the routing slip.
[Test]public async Task Test_the_routing_slip_activity(){ // Add the activity to the test harness await using var provider = new ServiceCollection() .AddMassTransitTestHarness(cfg => { cfg.AddActivity<MyActivity, MyActivityArgs, MyActivityLog>(); }) .BuildServiceProvider(true);
var harness = await provider.StartTestHarness();
var trackingNumber = NewId.NextGuid();
// Create a routing slip builder var builder = new RoutingSlipBuilder(trackingNumber);
var activityAddress = harness.GetExecuteActivityAddress<MyActivity, MyActivityArgs>(); builder.AddActivity("test", activityAddress, new { OrderNumber = "ORDER123" })
// Execute the routing slip await harness.Bus.Execute(builder.Build())
// Verify the activity completed Assert.That(await harness.Published.Any<RoutingSlipActivityCompleted>(x => x.Message.TrackingNumber == trackingNumber));
// Verify the routing slip completed Assert.That(await harness.Published.Any<RoutingSlipCompleted>(x => x.Message.TrackingNumber == trackingNumber));}