MethodCache.Testing 1.0.0-rc.15

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

MethodCache.Testing

Helpers for tests of the caching itself: a fake clock, a recorder that counts how often your implementation ran and what was invalidated, a check that every cached service resolves to its caching decorator, and a check that every cache tag has something that invalidates it.

Install it in the test project, at the same version as MethodCache.

dotnet add package MethodCache.Testing

Recorder and fake clock

AddMethodCacheTesting() registers a FakeTimeProvider as the container's TimeProvider and a CacheRecorder. Build the provider as the application does and resolve both:

var services = new ServiceCollection();
services.AddSingleton<Database>();
services.AddScoped<IProductService, ProductService>();
services.AddMethodCache();
services.AddMethodCacheTesting();   // a fake clock and a recorder
await using var provider = services.BuildServiceProvider();
var recorder = provider.GetRequiredService<CacheRecorder>();
var time = provider.GetRequiredService<FakeTimeProvider>();
var products = provider.CreateScope().ServiceProvider.GetRequiredService<IProductService>();

await products.GetAsync(42);
await products.GetAsync(42);
Assert.Equal(1, recorder.Loads<IProductService>(p => p.GetAsync(42, default)));   // the second call was a hit

await products.UpdateAsync(42, new Product(42, "Green tea", 5m));
Assert.Contains("product:42", recorder.InvalidatedTags);
await products.GetAsync(42);
Assert.Equal(2, recorder.Loads<IProductService>(p => p.GetAsync(42, default)));

time.Advance(TimeSpan.FromMinutes(11));   // past the Duration of 10 minutes
await products.GetAsync(42);
Assert.Equal(3, recorder.Loads<IProductService>(p => p.GetAsync(42, default)));

Loads counts the runs of one call, with its arguments; a hit doesn't count. InvalidatedTags and InvalidatedKeys list what [CacheInvalidate] methods, ICacheManager calls and MethodCache.EntityFrameworkCore invalidated, in order. Clear() forgets what a test's setup recorded. The clock starts at 2000-01-01 and only moves when the test advances it, so nothing expires on its own.

The recorder sees the built-in cache manager: with NoOpCacheManager or your own ICacheManager it records nothing.

Every cached service resolves

CachedServices.ProblemsAsync resolves each registered service with cached methods and checks that it is the generated caching decorator, so a service registered after AddMethodCache or one with a missing dependency fails a test instead of the application's startup:

// Every service with cached methods resolves, and to its generated caching decorator
Assert.Empty(await CachedServices.ProblemsAsync(provider));

When resolving the services needs a database or a key vault the test doesn't have, CachedServices.RegistrationProblems checks the registrations only and resolves nothing:

// Every service with cached methods is registered before AddMethodCache; resolves none of them
Assert.Empty(CachedServices.RegistrationProblems(provider));

Both throw InvalidOperationException when the provider has no service with cached methods, so a test with the wrong provider can't pass without checking anything.

Neither can see a service the application never registers. CachedServices.Registered lists the registered services with cached methods, ordered by full name, for a test to compare with the services the application should have:

// The application registers exactly these services with cached methods, none forgotten
Assert.Equal([typeof(IProductService)], CachedServices.Registered(provider));

A list written by hand goes stale when someone adds a cached interface and forgets to register it, which is the mistake the test is there for. CachedServices.Declared lists the interfaces with cached methods in the assemblies you pass, in the same order, so the test covers a new interface without a change:

// Every interface with cached methods in the application's project is registered
Assert.Equal(CachedServices.Declared(typeof(IProductService).Assembly), CachedServices.Registered(provider));

Every tag has an invalidation

TagCoverage.Uninvalidated lists the tags of the cached methods, from attributes, startup code and configuration, that nothing invalidates. It counts the [CacheInvalidate] methods of the registered services, the entity mapping of MethodCache.EntityFrameworkCore, and the invalidations in the projects the test project references:

// Every tag of a cached method has a [CacheInvalidate] method, an EF Core mapping or an invalidation in a referenced project
Assert.Empty(TagCoverage.Uninvalidated(provider));

MethodCache writes the tags each project invalidates into its assembly, and TagCoverage reads them from the assemblies next to the tests, so reference the projects that invalidate, such as a worker, from the test project. A tag marked InvalidatedElsewhere is covered only when one of them invalidates it; otherwise it is reported with MarkedInvalidatedElsewhere set. A tag marked InvalidatedManually, which an operator clears by hand, is covered without a check.

Name the tags invalidated where no referenced project shows it, such as by another application: TagCoverage.Uninvalidated(provider, alsoInvalidatedBy: ["price:{provider}:{model}"]). A placeholder matches any value, so "product:{id}" covers "product:{Id}" and "product:42".

Analyzer MC0028 checks the same within one project while you build; this check sees the whole application.

See Testing for more.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-rc.20 134 11/12/2026
1.0.0-rc.19 69 11/10/2026
1.0.0-rc.18 69 11/10/2026
1.0.0-rc.17 65 11/10/2026
1.0.0-rc.16 68 11/8/2026
1.0.0-rc.15 66 11/8/2026
1.0.0-rc.14 73 11/8/2026
1.0.0-rc.13 69 11/7/2026
1.0.0-rc.12 67 11/7/2026
1.0.0-rc.11 69 11/7/2026
1.0.0-rc.10 63 11/6/2026
1.0.0-rc.9 60 11/5/2026
1.0.0-rc.8 68 11/5/2026
1.0.0-rc.7 74 11/5/2026