Test helpers for .NET: a test base class that sends Serilog output to your test framework and captures log events for assertions, auto-mocking containers for FakeItEasy and NSubstitute, test data builders, and a harness for testing Roslyn source generators, analyzers and code fixes. TUnit and xUnit v3 are supported.
Documentation: docs/src/content/docs
Add the package for your test framework. It brings in Rocket.Surgery.Extensions.Testing:
# TUnit
dotnet add package Rocket.Surgery.Extensions.Testing.TUnit
# xUnit v3
dotnet add package Rocket.Surgery.Extensions.Testing.XUnit3
Derive the test class from LoggerTest<TContext>. Logger writes to the current test's output, and CaptureLogs
collects the events written until it is disposed:
using Serilog.Events;
using Rocket.Surgery.Extensions.Testing;
public class OrderServiceTests() : LoggerTest<TestRecord>(TestRecord.Create())
{
[Test]
public async Task Logs_a_warning_for_empty_orders()
{
using var capture = CaptureLogs(log => log.Level >= LogEventLevel.Warning, out var logs);
Logger.Information("not captured: below the filter");
Logger.Warning("order {OrderId} has no lines", 42);
await Assert.That(logs.Count()).IsEqualTo(1);
}
}
With Rocket.Surgery.Extensions.Testing.FakeItEasy, derive from AutoFakeTest<TContext> instead. AutoFake.Resolve<T>()
builds the class under test and fakes every dependency the container can't construct, returning the same fake each time
within a test:
using FakeItEasy;
using Rocket.Surgery.Extensions.Testing;
public class CheckoutTests() : AutoFakeTest<TestRecord>(TestRecord.Create())
{
[Test]
public async Task Charges_the_customer()
{
var payments = AutoFake.Resolve<IPaymentGateway>();
var checkout = AutoFake.Resolve<CheckoutService>();
await checkout.CompleteAsync(orderId: 42);
A.CallTo(() => payments.ChargeAsync(42, A<CancellationToken>._)).MustHaveHappenedOnceExactly();
}
}
See the quickstart, auto-mocking, fixtures and source generator testing pages.
| Package | NuGet | What it does |
|---|---|---|
| Rocket.Surgery.Extensions.Testing | LoggerTest<TContext>, which wires Serilog to the test output and captures log events, and the DryIoc helpers the mocking packages build on |
|
| Rocket.Surgery.Extensions.Testing.TUnit | TestRecord, the test context for TUnit |
|
| Rocket.Surgery.Extensions.Testing.XUnit3 | XUnitTestContext, the test context for xUnit v3 |
|
| Rocket.Surgery.Extensions.Testing.FakeItEasy | AutoFakeTest<TContext>: a DryIoc container that resolves the system under test and fakes missing dependencies with FakeItEasy |
|
| Rocket.Surgery.Extensions.Testing.NSubstitute | AutoSubstituteTest<TContext>: the same container with NSubstitute substitutes |
|
| Rocket.Surgery.Extensions.Testing.Fixtures | ITestFixtureBuilder and the With helpers for hand-written test data builders |
|
| Rocket.Surgery.Extensions.Testing.AutoFixtures | Source generator that writes a fixture builder from a constructor, marked with [AutoFixture] |
|
| Rocket.Surgery.Extensions.Testing.SourceGenerators | GeneratorTestContextBuilder: compiles sources, runs generators, analyzers, code fixes and refactorings, and snapshots the results with Verify |
Rocket.Surgery.Extensions.Testing.Analyzers comes in with Rocket.Surgery.Extensions.Testing, so you don't
reference it yourself. It ships no analyzer rules at present.
Run mise install before building: it installs the .NET SDK and the extra .NET 10 runtime that the net10.0 tests run on (.config/mise.toml). global.json pins the same SDK, so a dotnet outside mise fails fast unless that SDK is installed.
Note
On macOS, the first mise install of a new .NET SDK can overwrite the shared ~/.local/share/mise/dotnet-root/dotnet host in place, and macOS then kills every dotnet command (SIGKILL). Replace the host with a fresh copy to fix it:
cd ~/.local/share/mise/dotnet-root && cp -p dotnet dotnet.fresh && mv -f dotnet.fresh dotnet