Skip to content

Commit

Permalink
Better tests for keyed services
Browse files Browse the repository at this point in the history
  • Loading branch information
jodydonetti committed Jun 2, 2024
1 parent f5f6009 commit 2e6ccf7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,9 @@ public void CanActAsKeyedService()
});
services.AddFusionCache(quxCacheOriginal, true);

// USE [FromKeyedService] ATTRIBUTE
services.AddSingleton<SimpleServiceWithKeyedDependency>();

using var serviceProvider = services.BuildServiceProvider();

var cacheProvider = serviceProvider.GetRequiredService<IFusionCacheProvider>();
Expand All @@ -1078,6 +1081,8 @@ public void CanActAsKeyedService()
var quxCache = cacheProvider.GetCache("QuxCache");
var quxCache2 = serviceProvider.GetRequiredKeyedService<IFusionCache>("QuxCache");

var simpleService = serviceProvider.GetRequiredService<SimpleServiceWithKeyedDependency>();

Assert.NotNull(fooCache);
Assert.Equal(fooCache, fooCache2);

Expand All @@ -1092,6 +1097,9 @@ public void CanActAsKeyedService()
Assert.NotNull(quxCache);
Assert.Equal(quxCacheOriginal, quxCache);
Assert.Equal(quxCache, quxCache2);

Assert.NotNull(simpleService);
Assert.NotNull(simpleService);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using ZiggyCreatures.Caching.Fusion;

namespace FusionCacheTests.Stuff
{
public class SimpleServiceWithKeyedDependency
{
public SimpleServiceWithKeyedDependency([FromKeyedServices("FooCache")] IFusionCache cache)
{
System.ArgumentNullException.ThrowIfNull(cache);

Cache = cache;
}

public IFusionCache Cache { get; }
}
}

0 comments on commit 2e6ccf7

Please sign in to comment.