MagicCSharp.Testing 1.0.2

dotnet add package MagicCSharp.Testing --version 1.0.2
                    
NuGet\Install-Package MagicCSharp.Testing -Version 1.0.2
                    
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="MagicCSharp.Testing" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MagicCSharp.Testing" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MagicCSharp.Testing" />
                    
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 MagicCSharp.Testing --version 1.0.2
                    
#r "nuget: MagicCSharp.Testing, 1.0.2"
                    
#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 MagicCSharp.Testing@1.0.2
                    
#: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=MagicCSharp.Testing&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MagicCSharp.Testing&version=1.0.2
                    
Install as a Cake Tool

MagicCSharp.Testing

Test doubles for the framework's own seams, so a test of a use case is new PlaceOrderUseCase(...) and an assertion. No test framework dependency, no database, no containers — add MagicCSharp.Testing.Database when you need those.

The fakes for your interfaces — FakeOrdersRepository, a recording IEventDispatcher — are yours to write, a few lines each against the interface the use case asked for. What ships here is the doubles for the seams the framework owns:

Type Replaces Why
FakeTimeProvider TimeProvider .NET's own test clock, brought in by this package. Move time by hand; a thirty-day late fee is testable in milliseconds.
FakeKeyGen IKeyGenService Snowflake ids derived from the test's TimeProvider, so ids and timestamps agree.
SyncEventDispatcher IEventDispatcher Runs handlers inline and records them, so you can assert without sleeping.
InMemoryDistributedLockProvider IDistributedLockProvider Real mutual exclusion in-process, re-entrant during inline event dispatch.
TrackingDistributedLockProvider IDistributedLockProvider Grants everything, records the names — for asserting what was locked.
dotnet add package MagicCSharp.Testing

A use case, directly

var timeProvider = new FakeTimeProvider(new DateTimeOffset(2026, 3, 1, 0, 0, 0, TimeSpan.Zero));

var applyLateFees = new ApplyLateFeesUseCase(new FakeLeasesRepository(), timeProvider);

timeProvider.Advance(TimeSpan.FromDays(31));
await applyLateFees.Execute();

FakeTimeProvider comes from Microsoft's Microsoft.Extensions.TimeProvider.Testing, which this package references. SetUtcNow and Advance move it — forward only; it refuses to go back. Because it is .NET's own abstraction it also fakes what waits on time: Task.Delay(delay, timeProvider), PeriodicTimer and timed CancellationTokenSources complete when the test moves the clock, so a background service's loop can be tested without waiting for it.

Wiring a host

For a test that boots the application — WebApplicationFactory, or a ServiceCollection of your own — replace the framework's registrations:

var timeProvider = new FakeTimeProvider(new DateTimeOffset(2026, 3, 1, 0, 0, 0, TimeSpan.Zero));

services.RemoveAll<TimeProvider>();
services.AddSingleton<TimeProvider>(timeProvider);

services.RemoveAll<IKeyGenService>();
services.AddSingleton<IKeyGenService>(new FakeKeyGen(timeProvider));

services.RemoveAll<IDistributedLockProvider>();
services.AddSingleton<IDistributedLockProvider, InMemoryDistributedLockProvider>();

services.RemoveAll<IEventDispatcher>();
services.AddSingleton<IEventDispatcher>(sp =>
    new SyncEventDispatcher(sp.GetRequiredService<IAsyncEventDispatcher>()));

Asserting on events

await createOrder.Execute(new CreateOrderRequest { UserId = 1, ProductIds = [2, 3] });

Assert.True(eventDispatcher.HasDispatchedEvent<OrderCreatedEvent>());
Assert.Single(eventDispatcher.GetDispatchedEvents<OrderCreatedEvent>());
Assert.True(eventDispatcher.HasDispatchedEvent<OrderCreatedEvent>(orderCreatedEvent => orderCreatedEvent.UserId == 1));

DispatchedEvents is everything in order; ClearDispatchedEvents() resets between phases of one test.

One deliberate difference from production

SyncEventDispatcher runs handlers inline, inside whatever lock the emitting use case holds. In production dispatch is fire-and-forget, so handlers run after that lock is released. InMemoryDistributedLockProvider closes the gap: while SyncEventDispatcher is dispatching, a handler re-acquiring a name the emitter holds gets a no-op handle rather than deadlocking. The window is scoped to that one dispatch, so genuinely concurrent flows still get ordinary mutual exclusion.

The whole picture, and the optional repository layout: github.com/MagicDoorInc/MagicCSharp. MIT.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on MagicCSharp.Testing:

Package Downloads
MagicCSharp.Testing.Database

Repository integration testing for MagicCSharp against a real PostgreSQL in Testcontainers: one container shared across the suite, a logical database per test class, schema created once, and tables truncated between tests. Separate from MagicCSharp.Testing because it needs Docker, xUnit and Npgsql.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 77 9/24/2026
1.0.1 79 9/24/2026
1.0.0 77 9/23/2026