|
| 1 | +using System; |
| 2 | +using FluentAssertions; |
| 3 | +using JetBrains.Annotations; |
| 4 | +using JsonApiDotNetCore; |
| 5 | +using JsonApiDotNetCoreExample.Startups; |
| 6 | +using Microsoft.AspNetCore.Hosting; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace JsonApiDotNetCoreExampleTests.UnitTests.Configuration |
| 12 | +{ |
| 13 | + public sealed class DependencyContainerRegistrationTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void Can_resolve_registered_services_from_example_project() |
| 17 | + { |
| 18 | + // Arrange |
| 19 | + IHostBuilder hostBuilder = CreateValidatingHostBuilder(); |
| 20 | + |
| 21 | + // Act |
| 22 | + using IHost _ = hostBuilder.Build(); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void Cannot_resolve_registered_services_with_conflicting_scopes() |
| 27 | + { |
| 28 | + // Arrange |
| 29 | + IHostBuilder hostBuilder = CreateValidatingHostBuilder(); |
| 30 | + |
| 31 | + hostBuilder.ConfigureServices(services => |
| 32 | + { |
| 33 | + services.AddScoped<SomeScopedService>(); |
| 34 | + services.AddSingleton<SomeSingletonService>(); |
| 35 | + }); |
| 36 | + |
| 37 | + // Act |
| 38 | + Action action = () => |
| 39 | + { |
| 40 | + using IHost _ = hostBuilder.Build(); |
| 41 | + }; |
| 42 | + |
| 43 | + // Assert |
| 44 | + action.Should().ThrowExactly<AggregateException>().WithMessage("Some services are not able to be constructed * " + |
| 45 | + "Singleton * Cannot consume scoped service *"); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void Cannot_resolve_registered_services_with_circular_dependency() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + IHostBuilder hostBuilder = CreateValidatingHostBuilder(); |
| 53 | + |
| 54 | + hostBuilder.ConfigureServices(services => |
| 55 | + { |
| 56 | + services.AddScoped<CircularServiceA>(); |
| 57 | + services.AddScoped<CircularServiceB>(); |
| 58 | + }); |
| 59 | + |
| 60 | + // Act |
| 61 | + Action action = () => |
| 62 | + { |
| 63 | + using IHost _ = hostBuilder.Build(); |
| 64 | + }; |
| 65 | + |
| 66 | + // Assert |
| 67 | + action.Should().ThrowExactly<AggregateException>().WithMessage("Some services are not able to be constructed * " + |
| 68 | + "A circular dependency was detected *"); |
| 69 | + } |
| 70 | + |
| 71 | + private static IHostBuilder CreateValidatingHostBuilder() |
| 72 | + { |
| 73 | + IHostBuilder hostBuilder = Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webBuilder => |
| 74 | + { |
| 75 | + webBuilder.UseStartup<Startup>(); |
| 76 | + |
| 77 | + webBuilder.UseDefaultServiceProvider(options => |
| 78 | + { |
| 79 | + options.ValidateScopes = true; |
| 80 | + options.ValidateOnBuild = true; |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + return hostBuilder; |
| 85 | + } |
| 86 | + |
| 87 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 88 | + private sealed class SomeSingletonService |
| 89 | + { |
| 90 | + // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local |
| 91 | + public SomeSingletonService(SomeScopedService scopedService) |
| 92 | + { |
| 93 | + ArgumentGuard.NotNull(scopedService, nameof(scopedService)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 98 | + private sealed class SomeScopedService |
| 99 | + { |
| 100 | + } |
| 101 | + |
| 102 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 103 | + private sealed class CircularServiceA |
| 104 | + { |
| 105 | + // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local |
| 106 | + public CircularServiceA(CircularServiceB serviceB) |
| 107 | + { |
| 108 | + ArgumentGuard.NotNull(serviceB, nameof(serviceB)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + [UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 113 | + private sealed class CircularServiceB |
| 114 | + { |
| 115 | + // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local |
| 116 | + public CircularServiceB(CircularServiceA serviceA) |
| 117 | + { |
| 118 | + ArgumentGuard.NotNull(serviceA, nameof(serviceA)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments