MethodCache.Testing
1.0.0-rc.7
See the version list below for details.
dotnet add package MethodCache.Testing --version 1.0.0-rc.7
NuGet\Install-Package MethodCache.Testing -Version 1.0.0-rc.7
<PackageReference Include="MethodCache.Testing" Version="1.0.0-rc.7" />
<PackageVersion Include="MethodCache.Testing" Version="1.0.0-rc.7" />
<PackageReference Include="MethodCache.Testing" />
paket add MethodCache.Testing --version 1.0.0-rc.7
#r "nuget: MethodCache.Testing, 1.0.0-rc.7"
#:package MethodCache.Testing@1.0.0-rc.7
#addin nuget:?package=MethodCache.Testing&version=1.0.0-rc.7&prerelease
#tool nuget:?package=MethodCache.Testing&version=1.0.0-rc.7&prerelease
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, 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 tag has an invalidation
TagCoverage.Uninvalidated lists the tags of the cached methods, from attributes, startup code and configuration,
that no [CacheInvalidate] method of a registered service invalidates. Name the tags your code invalidates in other
ways, such as with InvalidateTagAsync:
// Every tag of a cached method has a [CacheInvalidate] method, or is in the list
Assert.Empty(TagCoverage.Uninvalidated(provider, alsoInvalidatedBy: ["food-catalog"]));
A placeholder matches any value, so "product:{id}" covers "product:{Id}" and "product:42". With
MethodCache.EntityFrameworkCore, pass the mapping's tags:
var mapping = provider.GetRequiredService<EntityTagMapping<ShopDb>>();
// [(Product, ["product:{Id}", "category:{CategoryId}"], []), (OrderLine, ["top-sellers"], [])]
var entities = mapping.Entities;
// Every tag of a cached method is mapped, or has a [CacheInvalidate] method
Assert.Empty(TagCoverage.Uninvalidated(provider, alsoInvalidatedBy: mapping.AllTags));
Analyzer MC0028 checks the same within one project while you build; this check sees the whole application.
See Testing for more.
| Product | Versions 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. |
-
net10.0
- MethodCache (= 1.0.0-rc.7)
- Microsoft.Extensions.TimeProvider.Testing (>= 10.0.0)
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 |