Test using the Microsoft Web Application Factory
MassTransit’s test harness can be used with Microsoft’s Web Application Factory, allowing unit and/or integration testing of ASP.NET applications.
:sample{sample="web-application-factory"}
To configure MassTransit’s test harness for use with the Web Application Factory, call AddMassTransitTestHarness in the set up as shown below.
await using var application = new WebApplicationFactory<Program>() .WithWebHostBuilder(builder => builder.ConfigureServices(services => services.AddMassTransitTestHarness()));
var testHarness = application.Services.GetTestHarness();
using var client = application.CreateClient();
var orderId = NewId.NextGuid();
var submitOrderResponse = await client.PostAsync("/Order", JsonContent.Create(new Order{ OrderId = orderId}));
var consumerTestHarness = testHarness.GetConsumerHarness<SubmitOrderConsumer>();
Assert.That(await consumerTestHarness.Consumed.Any<SubmitOrder>(x => x.Context.Message.OrderId == orderId), Is.True);