|
| 1 | +using AwesomeAssertions; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 4 | +using Ninject.Web.AspNetCore.Test.Fakes; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Ninject.Web.AspNetCore.Test.Unit |
| 11 | +{ |
| 12 | + |
| 13 | +#if NET8_0_OR_GREATER |
| 14 | + |
| 15 | + public class ServiceProviderKeyedTest |
| 16 | + { |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void OptionalKeyedServiceCollectionExisting_CorrectServiceResolved() |
| 20 | + { |
| 21 | + var collection = new ServiceCollection(); |
| 22 | + collection.Add(new ServiceDescriptor(typeof(IWarrior),"Samurai", typeof(Samurai), ServiceLifetime.Transient)); |
| 23 | + collection.Add(new ServiceDescriptor(typeof(IWarrior), "Ninja", new Ninja("test"))); |
| 24 | + var kernel = CreateTestKernel(collection); |
| 25 | + var provider = CreateServiceProvider(kernel); |
| 26 | + |
| 27 | + provider.GetKeyedService(typeof(IWarrior), "Ninja").Should().NotBeNull().And.BeOfType(typeof(Ninja)); |
| 28 | + provider.GetKeyedService(typeof(IWarrior), "Samurai").Should().NotBeNull().And.BeOfType(typeof(Samurai)); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void OptionalKeyedNinjectDirectBindingExisting_CorrectServiceResolved() |
| 34 | + { |
| 35 | + var kernel = CreateTestKernel(); |
| 36 | + kernel.Bind<IWarrior>().To<Samurai>().WithMetadata(nameof(ServiceKey), new ServiceKey("Samurai")); |
| 37 | + kernel.Bind<IWarrior>().ToConstant(new Ninja("test")).WithMetadata(nameof(ServiceKey), new ServiceKey("Ninja")); |
| 38 | + var provider = CreateServiceProvider(kernel); |
| 39 | + |
| 40 | + provider.GetKeyedService(typeof(IWarrior), "Samurai").Should().NotBeNull().And.BeOfType(typeof(Samurai)); |
| 41 | + provider.GetKeyedService(typeof(IWarrior), "Ninja").Should().NotBeNull().And.BeOfType(typeof(Ninja)); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void OptionalKeyedNonExisting_SingleServiceResolvedToNull() |
| 46 | + { |
| 47 | + var kernel = CreateTestKernel(); |
| 48 | + var provider = CreateServiceProvider(kernel); |
| 49 | + |
| 50 | + provider.GetKeyedService(typeof(IWarrior), "Samurai").Should().BeNull(); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void OptionalExistingMultipleKeydServices_ResolvedQueriedAsList() |
| 55 | + { |
| 56 | + var kernel = CreateTestKernel(); |
| 57 | + kernel.Bind<IWarrior>().To<Samurai>().WithMetadata(nameof(ServiceKey), new ServiceKey("Samurai"));; |
| 58 | + kernel.Bind<IWarrior>().ToConstant(new Ninja("test")).WithMetadata(nameof(ServiceKey), new ServiceKey("Ninja")); |
| 59 | + var provider = CreateServiceProvider(kernel); |
| 60 | + |
| 61 | + var result = provider.GetService(typeof(IList<IWarrior>)) as IEnumerable<IWarrior>; |
| 62 | + |
| 63 | + result.Should().NotBeNull(); |
| 64 | + var resultList = result.ToList(); |
| 65 | + resultList.Should().HaveCount(2); |
| 66 | + resultList.Should().Contain(x => x is Samurai); |
| 67 | + resultList.Should().Contain(x => x is Ninja); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void ExistingMultipleServices_ResolvesNonKeyedToNull() |
| 72 | + { |
| 73 | + var kernel = CreateTestKernel(); |
| 74 | + kernel.Bind<IWarrior>().To<Samurai>().WithMetadata(nameof(ServiceKey), new ServiceKey("Samurai"));; |
| 75 | + kernel.Bind<IWarrior>().ToConstant(new Ninja("test")).WithMetadata(nameof(ServiceKey), new ServiceKey("Ninja")); |
| 76 | + var provider = CreateServiceProvider(kernel); |
| 77 | + |
| 78 | + provider.GetService(typeof(IWarrior)).Should().BeNull(); |
| 79 | + } |
| 80 | + |
| 81 | + private IServiceProvider CreateServiceProvider(AspNetCoreKernel kernel) |
| 82 | + { |
| 83 | + NinjectServiceProviderBuilder builder = CreateServiceProviderBuilder(kernel); |
| 84 | + var provider = builder.Build(); |
| 85 | + return provider; |
| 86 | + } |
| 87 | + |
| 88 | + private NinjectServiceProviderBuilder CreateServiceProviderBuilder(AspNetCoreKernel kernel) |
| 89 | + { |
| 90 | + var collection = new ServiceCollection(); |
| 91 | + var factory = new NinjectServiceProviderFactory(kernel); |
| 92 | + var builder = factory.CreateBuilder(collection); |
| 93 | + return builder; |
| 94 | + } |
| 95 | + |
| 96 | + private AspNetCoreKernel CreateTestKernel(IServiceCollection collection = null) |
| 97 | + { |
| 98 | + var kernel = new AspNetCoreKernel(new NinjectSettings() { LoadExtensions = false }); |
| 99 | + kernel.Load(typeof(AspNetCoreApplicationPlugin).Assembly); |
| 100 | + if (collection != null) |
| 101 | + { |
| 102 | + new ServiceCollectionAdapter().Populate(kernel, collection); |
| 103 | + } |
| 104 | + |
| 105 | + return kernel; |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | +#endif |
| 110 | +} |
0 commit comments