Sitelet https://github.com/Swevo/MassTransit.RequestClient.TestKit
Skip to content

About

FakeRequestClient<T> and FakePublishEndpoint for unit-testing MassTransit consumers and handlers without a real bus.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Swevo.MassTransit.TestKit

[![NuGet](https://img.shields.io/nuget/v/Swevo.MassTransit.TestKit NuGet Downloads.svg)](https://www.nuget.org/packages/Swevo.MassTransit.TestKit/) Build License: MIT .NET 10 Ready

Lightweight test doubles for MassTransit request/response patterns. Drop-in fakes for IRequestClient<T> and IPublishEndpoint — no harness, no bus, no infrastructure.


Why?

MassTransit.Testing's InMemoryTestHarness is great for integration tests, but unit tests shouldn't need a bus. This package gives you:

Type Replaces
FakeRequestClient<TRequest> IRequestClient<TRequest>
FakePublishEndpoint IPublishEndpoint

Installation

dotnet add package Swevo.MassTransit.TestKit

Quick Start

FakeRequestClient — Single Response

var client = new FakeRequestClient<GetDevice>()
    .RespondWith(new DeviceFound(42, "Thermostat"));

var response = await client.GetResponse<DeviceFound>(new GetDevice(42));

response.Message.DeviceId.Should().Be(42);
client.WasCalled.Should().BeTrue();

FakeRequestClient — Union Response

var client = new FakeRequestClient<PlaceOrder>()
    .RespondWith(new OrderAccepted(orderId));

var (accepted, rejected) = await client.GetResponse<OrderAccepted, OrderRejected>(
    new PlaceOrder(orderId, 99.99m));

(await accepted).Message.OrderId.Should().Be(orderId);
rejected.IsCompleted.Should().BeFalse(); // non-matching branch never completes

FakeRequestClient — Exception

var client = new FakeRequestClient<GetDevice>()
    .Throws(new TimeoutException());

Func<Task> act = () => client.GetResponse<DeviceFound>(new GetDevice(42));
await act.Should().ThrowAsync<TimeoutException>();

FakePublishEndpoint

var publish = new FakePublishEndpoint();

await publish.Publish(new DeviceRegistered(42));

publish.WasPublished<DeviceRegistered>().Should().BeTrue();
publish.MostRecent<DeviceRegistered>()!.DeviceId.Should().Be(42);

API Reference

FakeRequestClient<TRequest>

Member Description
.RespondWith(message) Configure the response message (fluent)
.Throws(exception) Configure a thrown exception (fluent)
WasCalled true if any request was sent
CallCount Number of requests sent
MostRecentRequest Last request message sent
ReceivedRequests All requests in order

FakePublishEndpoint

Member Description
GetMessages<T>() All published messages of type T
WasPublished<T>() true if any message of type T was published
MostRecent<T>() Most recently published message of type T
Clear() Reset all captured messages

Usage in SCM

The SCM codebase uses testHarness.Value.GetRequestClient<T>() in WebApiClientDriver. For unit tests that don't need the full harness, swap in a fake:

// Instead of starting the web host:
var client = new FakeRequestClient<GetDevice>()
    .RespondWith(new DeviceFound(deviceId, "Thermostat"));

var handler = new GetDeviceQueryHandler(client);
var result = await handler.Handle(new GetDeviceQuery(deviceId), CancellationToken.None);

Compatibility

Package Version
MassTransit 9.x
.NET net8.0+

Also by the same author

🌐 Full suite overview: swevo.github.io

Package Description
AutoLog.Generator Compile-time high-performance logging — [Log(Level, Message)] generates LoggerMessage.Define. AOT-safe.
AutoHttpClient.Generator Compile-time typed HTTP client — [HttpClient] on an interface generates a strongly-typed client. AOT-safe Refit alternative.
AutoDispatch.Generator Compile-time CQRS dispatcher — [Handler] generates a strongly-typed IDispatcher. No MediatR, no reflection.
AutoWire Compile-time DI auto-registration — [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code.
AutoMap.Generator Compile-time object mapping with generated extension methods. AOT-safe AutoMapper alternative.

Related Packages

Package Downloads Description
Swevo.HttpClient.TestKit Downloads FakeHttpMessageHandler for unit-testing HttpClient calls without mocking frameworks
Swevo.AutoTestData Downloads Compile-time test data builders for

License

MIT © 2025 Justin Bannister

About

FakeRequestClient<T> and FakePublishEndpoint for unit-testing MassTransit consumers and handlers without a real bus.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages