Skip to content
RocketSurgeonsGuildPublic

Latest commit

 

History

2,623 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rocket Surgery Testing

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.

github-release-badge github-license-badge github-build-badge codecov-badge nuget-Rocket.Surgery.Extensions.Testing-badge

Documentation: docs/src/content/docs

Getting started

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.

Packages

Package NuGet What it does
Rocket.Surgery.Extensions.Testing nuget-Rocket.Surgery.Extensions.Testing-badge 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 nuget-Rocket.Surgery.Extensions.Testing.TUnit-badge TestRecord, the test context for TUnit
Rocket.Surgery.Extensions.Testing.XUnit3 nuget-Rocket.Surgery.Extensions.Testing.XUnit3-badge XUnitTestContext, the test context for xUnit v3
Rocket.Surgery.Extensions.Testing.FakeItEasy nuget-Rocket.Surgery.Extensions.Testing.FakeItEasy-badge AutoFakeTest<TContext>: a DryIoc container that resolves the system under test and fakes missing dependencies with FakeItEasy
Rocket.Surgery.Extensions.Testing.NSubstitute nuget-Rocket.Surgery.Extensions.Testing.NSubstitute-badge AutoSubstituteTest<TContext>: the same container with NSubstitute substitutes
Rocket.Surgery.Extensions.Testing.Fixtures nuget-Rocket.Surgery.Extensions.Testing.Fixtures-badge ITestFixtureBuilder and the With helpers for hand-written test data builders
Rocket.Surgery.Extensions.Testing.AutoFixtures nuget-Rocket.Surgery.Extensions.Testing.AutoFixtures-badge Source generator that writes a fixture builder from a constructor, marked with [AutoFixture]
Rocket.Surgery.Extensions.Testing.SourceGenerators nuget-Rocket.Surgery.Extensions.Testing.SourceGenerators-badge 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.

Contributing

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

Releases

Packages

Used by

Contributors

Languages