Swevo.MassTransit.TestKit 1.0.1

Prefix Reserved
dotnet add package Swevo.MassTransit.TestKit --version 1.0.1
                    
NuGet\Install-Package Swevo.MassTransit.TestKit -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Swevo.MassTransit.TestKit" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swevo.MassTransit.TestKit" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Swevo.MassTransit.TestKit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Swevo.MassTransit.TestKit --version 1.0.1
                    
#r "nuget: Swevo.MassTransit.TestKit, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Swevo.MassTransit.TestKit@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Swevo.MassTransit.TestKit&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Swevo.MassTransit.TestKit&version=1.0.1
                    
Install as a Cake Tool

Swevo.MassTransit.TestKit

NuGet Build License: MIT

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+

License

MIT © 2025 Justin Bannister

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 121 6/26/2026
1.0.0 94 6/26/2026

1.0.0: Initial release. FakeRequestClient with single, union, and dynamic-factory response modes. FakePublishEndpoint with typed message capture.