microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Core.UnitTests/Hosting/ResolveFromServicesPreHostTests.cs
152lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.Configuration; |
| 5 | using Microsoft.Extensions.DependencyInjection; |
| 6 | using Microsoft.Extensions.Logging; |
| 7 | using Microsoft.Extensions.Logging.Abstractions; |
| 8 | using Microsoft.Teams.Core.Hosting; |
| 9 | |
| 10 | namespace Microsoft.Teams.Core.UnitTests.Hosting; |
| 11 | |
| 12 | public class ResolveFromServicesPreHostTests |
| 13 | { |
| 14 | // --- ResolveFromServicesPreHost --- |
| 15 | |
| 16 | [Fact] |
| 17 | public void ResolveFromServicesPreHost_WithNoRegistration_ReturnsNull() |
| 18 | { |
| 19 | // Arrange |
| 20 | ServiceCollection services = new(); |
| 21 | |
| 22 | // Act |
| 23 | IConfiguration? result = AddBotApplicationExtensions.ResolveFromServicesPreHost<IConfiguration>(services); |
| 24 | |
| 25 | // Assert |
| 26 | Assert.Null(result); |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public void ResolveFromServicesPreHost_WithDirectInstance_ReturnsInstance() |
| 31 | { |
| 32 | // Arrange |
| 33 | ServiceCollection services = new(); |
| 34 | IConfigurationRoot configuration = new ConfigurationBuilder().Build(); |
| 35 | services.AddSingleton<IConfiguration>(configuration); |
| 36 | |
| 37 | // Act |
| 38 | IConfiguration? result = AddBotApplicationExtensions.ResolveFromServicesPreHost<IConfiguration>(services); |
| 39 | |
| 40 | // Assert |
| 41 | Assert.Same(configuration, result); |
| 42 | } |
| 43 | |
| 44 | [Fact] |
| 45 | public void ResolveFromServicesPreHost_WithFactoryRegistration_ResolvesViaProvider() |
| 46 | { |
| 47 | // Arrange |
| 48 | ServiceCollection services = new(); |
| 49 | IConfigurationRoot configuration = new ConfigurationBuilder().Build(); |
| 50 | services.AddSingleton<IConfiguration>(_ => configuration); |
| 51 | |
| 52 | // Act |
| 53 | IConfiguration? result = AddBotApplicationExtensions.ResolveFromServicesPreHost<IConfiguration>(services); |
| 54 | |
| 55 | // Assert |
| 56 | Assert.NotNull(result); |
| 57 | } |
| 58 | |
| 59 | [Fact] |
| 60 | public void ResolveFromServicesPreHost_WithMultipleRegistrations_ReturnsLast() |
| 61 | { |
| 62 | // Arrange — DI last-registration-wins behavior |
| 63 | ServiceCollection services = new(); |
| 64 | IConfigurationRoot first = new ConfigurationBuilder().Build(); |
| 65 | IConfigurationRoot second = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?> { ["key"] = "value" }).Build(); |
| 66 | services.AddSingleton<IConfiguration>(first); |
| 67 | services.AddSingleton<IConfiguration>(second); |
| 68 | |
| 69 | // Act |
| 70 | IConfiguration? result = AddBotApplicationExtensions.ResolveFromServicesPreHost<IConfiguration>(services); |
| 71 | |
| 72 | // Assert |
| 73 | Assert.Same(second, result); |
| 74 | } |
| 75 | |
| 76 | // --- LogFromServices --- |
| 77 | |
| 78 | [Fact] |
| 79 | public void LogFromServices_WithNoLoggerFactory_CallsActionWithNullLogger() |
| 80 | { |
| 81 | // Arrange |
| 82 | ServiceCollection services = new(); |
| 83 | ILogger? captured = null; |
| 84 | |
| 85 | // Act |
| 86 | AddBotApplicationExtensions.LogFromServices(services, l => captured = l); |
| 87 | |
| 88 | // Assert |
| 89 | Assert.Same(NullLogger.Instance, captured); |
| 90 | } |
| 91 | |
| 92 | [Fact] |
| 93 | public void LogFromServices_WithAddLogging_CallsActionWithRealLogger() |
| 94 | { |
| 95 | // Arrange — typical ASP.NET Core registration via factory delegate |
| 96 | ServiceCollection services = new(); |
| 97 | services.AddLogging(); |
| 98 | ILogger? captured = null; |
| 99 | |
| 100 | // Act |
| 101 | AddBotApplicationExtensions.LogFromServices(services, l => captured = l); |
| 102 | |
| 103 | // Assert |
| 104 | Assert.NotNull(captured); |
| 105 | Assert.IsNotType<NullLogger>(captured); |
| 106 | } |
| 107 | |
| 108 | [Fact] |
| 109 | public void LogFromServices_WithAddLogging_LoggingDoesNotThrow() |
| 110 | { |
| 111 | // Arrange — validates that logging within the action does not throw ObjectDisposedException |
| 112 | // even though the temporary ServiceProvider is disposed after the action completes |
| 113 | ServiceCollection services = new(); |
| 114 | services.AddLogging(); |
| 115 | |
| 116 | // Act / Assert — should not throw |
| 117 | AddBotApplicationExtensions.LogFromServices(services, l => l.LogInformation("test message")); |
| 118 | } |
| 119 | |
| 120 | [Fact] |
| 121 | public void LogFromServices_WithDirectInstance_CallsActionWithRealLogger() |
| 122 | { |
| 123 | // Arrange |
| 124 | ServiceCollection services = new(); |
| 125 | LoggerFactory factory = new(); |
| 126 | services.AddSingleton<ILoggerFactory>(factory); |
| 127 | ILogger? captured = null; |
| 128 | |
| 129 | // Act |
| 130 | AddBotApplicationExtensions.LogFromServices(services, l => captured = l); |
| 131 | |
| 132 | // Assert |
| 133 | Assert.NotNull(captured); |
| 134 | Assert.IsNotType<NullLogger>(captured); |
| 135 | } |
| 136 | |
| 137 | [Fact] |
| 138 | public void LogFromServices_WithCustomCategory_UsesCategoryType() |
| 139 | { |
| 140 | // Arrange |
| 141 | ServiceCollection services = new(); |
| 142 | services.AddLogging(); |
| 143 | ILogger? captured = null; |
| 144 | |
| 145 | // Act |
| 146 | AddBotApplicationExtensions.LogFromServices(services, l => captured = l, typeof(ResolveFromServicesPreHostTests)); |
| 147 | |
| 148 | // Assert |
| 149 | Assert.NotNull(captured); |
| 150 | Assert.IsNotType<NullLogger>(captured); |
| 151 | } |
| 152 | } |
| 153 | |