Add integration test project with MediatR procedure tests
Added ReC.Tests project with integration tests for stored procedure commands and queries via MediatR, covering endpoints, actions, results, and profiles. Introduced a test base for DI/configuration, helper for private property access, and updated the solution to include the new test suite. Tests use NUnit and cover both positive and negative scenarios.
This commit is contained in:
73
tests/ReC.Tests/Application/RecApplicationTestBase.cs
Normal file
73
tests/ReC.Tests/Application/RecApplicationTestBase.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ReC.Application;
|
||||
using ReC.Application.Common.Options;
|
||||
using ReC.Infrastructure;
|
||||
|
||||
namespace ReC.Tests.Application;
|
||||
|
||||
public abstract class RecApplicationTestBase : IDisposable
|
||||
{
|
||||
protected RecApplicationTestBase()
|
||||
{
|
||||
Configuration = BuildConfiguration();
|
||||
ServiceProvider = BuildServiceProvider(Configuration);
|
||||
}
|
||||
|
||||
protected IConfiguration Configuration { get; }
|
||||
|
||||
protected IServiceProvider ServiceProvider { get; }
|
||||
|
||||
private static IConfiguration BuildConfiguration()
|
||||
{
|
||||
var appSettingsPath = LocateApiAppSettings();
|
||||
|
||||
return new ConfigurationBuilder()
|
||||
.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: false)
|
||||
.Build();
|
||||
}
|
||||
|
||||
private static IServiceProvider BuildServiceProvider(IConfiguration configuration)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddSingleton(configuration);
|
||||
|
||||
services.AddRecServices(options =>
|
||||
{
|
||||
options.LuckyPennySoftwareLicenseKey = configuration["LuckyPennySoftwareLicenseKey"];
|
||||
options.ConfigureRecActions(configuration.GetSection("RecAction"));
|
||||
});
|
||||
|
||||
services.AddRecInfrastructure(opt =>
|
||||
{
|
||||
opt.ConfigureDbContext((_, builder) => builder.UseSqlServer(configuration.GetConnectionString("Default")));
|
||||
});
|
||||
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private static string LocateApiAppSettings()
|
||||
{
|
||||
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (current is not null)
|
||||
{
|
||||
var candidate = Path.Combine(current.FullName, "src", "ReC.API", "appsettings.json");
|
||||
if (File.Exists(candidate))
|
||||
return candidate;
|
||||
|
||||
current = current.Parent;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("Could not locate src/ReC.API/appsettings.json from test base directory.");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (ServiceProvider is IDisposable disposable)
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user