Swevo.MassTransit.TestKit
1.0.2
Prefix Reserved
dotnet add package Swevo.MassTransit.TestKit --version 1.0.2
NuGet\Install-Package Swevo.MassTransit.TestKit -Version 1.0.2
<PackageReference Include="Swevo.MassTransit.TestKit" Version="1.0.2" />
<PackageVersion Include="Swevo.MassTransit.TestKit" Version="1.0.2" />
<PackageReference Include="Swevo.MassTransit.TestKit" />
paket add Swevo.MassTransit.TestKit --version 1.0.2
#r "nuget: Swevo.MassTransit.TestKit, 1.0.2"
#:package Swevo.MassTransit.TestKit@1.0.2
#addin nuget:?package=Swevo.MassTransit.TestKit&version=1.0.2
#tool nuget:?package=Swevo.MassTransit.TestKit&version=1.0.2
Swevo.MassTransit.TestKit
[](https://www.nuget.org/packages/Swevo.MassTransit.TestKit/)
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 | FakeHttpMessageHandler for unit-testing HttpClient calls without mocking frameworks | |
| Swevo.AutoTestData | Compile-time test data builders for |
License
MIT © 2025 Justin Bannister
| Product | Versions 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 is compatible. 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. |
-
net10.0
- MassTransit (>= 9.2.0)
-
net8.0
- MassTransit (>= 9.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.2: Add net10.0 target framework support. 1.0.0: Initial release. FakeRequestClient with single, union, and dynamic-factory response modes. FakePublishEndpoint with typed message capture.