MagicCSharp.Testing 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package MagicCSharp.Testing --version 1.1.3
                    
NuGet\Install-Package MagicCSharp.Testing -Version 1.1.3
                    
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.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MagicCSharp.Testing" Version="1.1.3" />
                    
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.1.3
                    
#r "nuget: MagicCSharp.Testing, 1.1.3"
                    
#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.1.3
                    
#: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.1.3
                    
Install as a Cake Addin
#tool nuget:?package=MagicCSharp.Testing&version=1.1.3
                    
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.

And three helpers for your own fakes: InMemoryRepository and InMemorySoftDeleteRepository, a repository fake that behaves like the EF bases; TestServices, which builds the use case under test from DI with your fakes in it; and [GenerateFakeBase], a fake whose every member throws until a test scripts it.

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.

An in-memory repository

A fake repository is mostly the same code every time: a dictionary, the next id, the filter. Derive from InMemoryRepository<TEntity, TEdit, TFilter> (or InMemorySoftDeleteRepository for soft delete) and implement your repository interface:

public class ParcelsRepository(TimeProvider timeProvider, long firstId = 1)
    : InMemorySoftDeleteRepository<Parcel, ParcelEdit, ParcelFilter>(timeProvider, firstId), IParcelsRepository
{
    protected override IReadOnlyCollection<string> SetOncePropertyNames
    {
        get { return [nameof(Parcel.WarehouseId)]; }
    }
}

It behaves as BaseIdRepository and BaseIdPaginatedRepository do on the database: reads newest first; Created and Updated from the TimeProvider, Updated only when something changed; an update or delete of a missing id throws NotFoundIdException; a bulk call checks every id first and writes nothing on a miss; paging honours IsDisabled; an update leaves the columns SetOncePropertyNames lists unchanged (none by default), as a DAL's Apply leaves the columns set once at creation — for a generated Apply, the DAL's required and [Key] properties; list those here. A filter is applied through its Matches, which the generator writes for a [GenerateMatches] filter listing IEntityFilter<TEntity> (MagicCSharp.Data), so the fake narrows exactly as the generated query does.

SeedStoredRow(edit) stores a row as the database already holds it, without going through Create or its overrides; StoredRowsByIdAscending is every row, soft-deleted too. The entity needs a public long Id with a setter or init; the constructor throws naming it otherwise.

When to write it by hand: override the one member whose behaviour differs (CreateEntity, CopyWithEdit, Matches, ApplyOrder, CreateNotFoundException, or any interface member — all are virtual), not the whole class. A filter without IEntityFilter needs a Matches override, or the first filtered read throws naming the filter. A fake for a key repository, or one whose store is not rows, is a class of your own.

The use case from DI

TestServices builds what the test calls the way the host does, with the fakes you give it, so adding a dependency to a use case does not mean editing every test that constructs it:

var testServices = new TestServices(timeProvider)
    .AddFake<IParcelsRepository>(parcelsRepository)
    .AddRegistrations(services => services.AddMagicUseCases(ThisAssemblyOnly));

var shelvedParcel = await testServices.Get<IShelveParcelUseCase>().Execute(parcel.Id);

The TimeProvider you pass is registered; loggers are NullLoggers unless you register others. AddFake registers one instance (a later one for the same service replaces it); AddRegistrations takes any other registration. Get<T>() resolves from one scope, building the container on the first call, and a missing dependency throws naming it. Create<T>(dependencies) builds one instance with the given objects in the constructor parameters they fit and the rest from the container. Registering after the first Get or Create throws. Resolving through the interface also runs any [LogExecuting] decorator.

A generated fake base

With MagicCSharp.Generators referenced, one line gives an interface a fake base whose every member is virtual and throws NotScriptedException ("IOrdersClient.GetOrders(OrderFilter) is not scripted"):

[GenerateFakeBase(typeof(IOrdersClient))]
public abstract partial class FakeOrdersClientBase;

public class FakeOrdersClient : FakeOrdersClientBase
{
    public override Task<OrderDto?> GetOrder(long orderId)
    {
        ...
    }
}

The fake overrides only what its tests use. A member added to the interface later breaks no fake, and a test that calls a member nobody scripted fails loudly instead of passing on an empty answer. The fake base keeps any base class you give it. Any interface works, not only clients; a member the class or its bases already implement is left alone.

When to write it by hand: an interface with a generic method is refused (MCS1515); write that fake as a class that implements the interface. To leave the generated base, write the throwing members yourself and remove the attribute.

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; and a check that a context's migrations upgrade its last released schema. 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.1.4 84 11/22/2026
1.1.3 72 11/22/2026
1.1.2 76 11/22/2026
1.1.1 79 11/21/2026
1.1.0 72 11/21/2026
1.0.3 168 10/6/2026
1.0.2 137 9/24/2026
1.0.1 139 9/24/2026
1.0.0 129 9/23/2026