Add tests for ReCClient dependency injection setup

Added a new `DependencyInjectionTests` class to validate the
dependency injection setup for the `ReCClient` class.

- Added tests to ensure `ReCClient` can be resolved when registered
  with a base URL or custom HTTP client configuration.
- Verified default options are registered when no callback is
  supplied and that options callbacks are applied correctly.
- Added tests to validate behavior when `LogSuccessfulRequests`
  is enabled, including scenarios with and without a registered
  logger.
- Included necessary `using` directives for DI, logging, options,
  HTTP client, and the `ReC.Client` namespace.
This commit is contained in:
2026-05-21 09:28:57 +02:00
parent ce5ffaae44
commit 7298140648

View File

@@ -0,0 +1,115 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ReC.Client;
using System.Net.Http;
namespace ReC.Tests.Client;
[TestFixture]
public class DependencyInjectionTests
{
[Test]
public void AddRecClient_with_base_url_registers_resolvable_client()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddRecClient("https://example.invalid/");
using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<ReCClient>();
Assert.That(client, Is.Not.Null);
Assert.That(client.RecActions, Is.Not.Null);
Assert.That(client.Results, Is.Not.Null);
Assert.That(client.Profiles, Is.Not.Null);
Assert.That(client.EndpointAuth, Is.Not.Null);
Assert.That(client.EndpointParams, Is.Not.Null);
Assert.That(client.Endpoints, Is.Not.Null);
Assert.That(client.Common, Is.Not.Null);
}
[Test]
public void AddRecClient_with_configure_client_registers_resolvable_client()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddRecClient(http =>
{
http.BaseAddress = new Uri("https://example.invalid/");
http.Timeout = TimeSpan.FromSeconds(5);
});
using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<ReCClient>();
Assert.That(client, Is.Not.Null);
}
[Test]
public void AddRecClient_registers_default_options_when_no_callback_supplied()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddRecClient("https://example.invalid/");
using var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<ReCClientOptions>>().Value;
Assert.That(options, Is.Not.Null);
Assert.That(options.LogSuccessfulRequests, Is.True, "Default value of LogSuccessfulRequests should be true.");
}
[Test]
public void AddRecClient_applies_options_callback()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true);
using var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<ReCClientOptions>>().Value;
Assert.That(options.LogSuccessfulRequests, Is.True);
}
[Test]
public void ReCClient_constructor_throws_when_LogSuccessfulRequests_enabled_but_logger_is_null()
{
// Microsoft.Extensions.Http always registers an ILoggerFactory, so the
// "logger == null" branch can only be exercised by invoking the constructor directly.
var services = new ServiceCollection();
services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true);
using var provider = services.BuildServiceProvider();
var httpClientFactory = provider.GetRequiredService<IHttpClientFactory>();
var options = provider.GetRequiredService<IOptions<ReCClientOptions>>();
var ex = Assert.Throws<InvalidOperationException>(
() => new ReCClient(httpClientFactory, options, logger: null));
Assert.That(ex!.Message, Does.Contain(nameof(ReCClientOptions.LogSuccessfulRequests)));
Assert.That(ex.Message, Does.Contain("ILogger"));
}
[Test]
public void ReCClient_resolves_when_LogSuccessfulRequests_enabled_and_logger_registered()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true);
using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<ReCClient>();
Assert.That(client, Is.Not.Null);
}
}