refactor(Mock): Host-Klasse für csetup erstellen und MediatR integrieren
- Migration zu Microsoft.Extensions.Hosting.CreateDefaultBuilder - Optionale Parameter und echte DB-Unterstützung in Tests entfernt - InMemoryDatabase zum Testen hinzugefügt - MediatR für die Befehlsverarbeitung integriert - Beispielmethode zum Initialisieren von Empfängern im Test-Host hinzugefügt
This commit is contained in:
parent
6863ada4be
commit
baf2207d03
@ -1,5 +1,4 @@
|
||||
using EnvelopeGenerator.Application.Dto;
|
||||
using EnvelopeGenerator.Application.Envelopes.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
@ -1,78 +1,19 @@
|
||||
using EnvelopeGenerator.Application;
|
||||
using EnvelopeGenerator.Application.Histories.Commands;
|
||||
using EnvelopeGenerator.Application.Histories.Commands;
|
||||
using EnvelopeGenerator.Application.Histories.Queries;
|
||||
using EnvelopeGenerator.Application.Receivers.Commands;
|
||||
using EnvelopeGenerator.Domain;
|
||||
using EnvelopeGenerator.Infrastructure;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace EnvelopeGenerator.Tests.Application;
|
||||
|
||||
[TestFixture]
|
||||
public class HistoryTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
private IMediator Mediator => _host.Services.GetRequiredService<IMediator>();
|
||||
|
||||
private readonly List<(int Id, string EmailAddress)> _receivers = new();
|
||||
|
||||
public (int Id, string EmailAddress) Receiver => _receivers.Count == 0
|
||||
? throw new InvalidOperationException("Receiver list is empty.")
|
||||
: _receivers[Random.Shared.Next(_receivers.Count)];
|
||||
private Mock.Host _host;
|
||||
|
||||
[SetUp]
|
||||
public async Task Setup()
|
||||
{
|
||||
_host = Host.CreateDefaultBuilder()
|
||||
.ConfigureAppConfiguration((context, config) =>
|
||||
{
|
||||
// add appsettings.json
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
IConfiguration configuration = context.Configuration;
|
||||
|
||||
// add Application and Infrastructure services
|
||||
#pragma warning disable CS0618
|
||||
services.AddEnvelopeGeneratorServices(configuration);
|
||||
services.AddEnvelopeGeneratorInfrastructureServices(
|
||||
(sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"),
|
||||
context.Configuration
|
||||
);
|
||||
#pragma warning restore CS0618
|
||||
})
|
||||
.Build();
|
||||
|
||||
// set receivers
|
||||
var receivers = await Task.WhenAll(
|
||||
new[]
|
||||
{
|
||||
"max.mueller@email.de",
|
||||
"anna.schmidt@email.de",
|
||||
"lukas.schneider@email.de",
|
||||
"sophia.fischer@email.de",
|
||||
"jonas.weber@email.de",
|
||||
"lea.hoffmann@email.de",
|
||||
"felix.wagner@email.de",
|
||||
"mia.becker@email.de",
|
||||
"paul.schulz@email.de",
|
||||
"lena.koch@email.de"
|
||||
}
|
||||
.Select(async email =>
|
||||
{
|
||||
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
||||
var (Id, AlreadyExists) = await Mediator.Send(cmd);
|
||||
return (Id, email);
|
||||
})
|
||||
);
|
||||
_receivers.AddRange(receivers);
|
||||
_host = Mock.CreateHost();
|
||||
await _host.AddSampleReceivers();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@ -94,14 +35,14 @@ public class HistoryTests
|
||||
};
|
||||
|
||||
// Act
|
||||
var id = await Mediator.Send(createCmd);
|
||||
var id = await _host.Mediator.Send(createCmd);
|
||||
|
||||
// Assert
|
||||
Assert.That(id, Is.Not.Null);
|
||||
|
||||
// ReadHistory query
|
||||
var query = new ReadHistoryQuery(1);
|
||||
var result = await Mediator.Send(query);
|
||||
var result = await _host.Mediator.Send(query);
|
||||
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
}
|
||||
@ -124,11 +65,11 @@ public class HistoryTests
|
||||
Status = Constants.EnvelopeStatus.EnvelopePartlySigned
|
||||
};
|
||||
|
||||
await Mediator.Send(createCmd1);
|
||||
await Mediator.Send(createCmd2);
|
||||
await _host.Mediator.Send(createCmd1);
|
||||
await _host.Mediator.Send(createCmd2);
|
||||
|
||||
// Act
|
||||
var result = await Mediator.Send(new ReadHistoryQuery(2, Constants.EnvelopeStatus.EnvelopePartlySigned));
|
||||
var result = await _host.Mediator.Send(new ReadHistoryQuery(2, Constants.EnvelopeStatus.EnvelopePartlySigned));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Has.Exactly(1).Items);
|
||||
@ -140,7 +81,7 @@ public class HistoryTests
|
||||
public async Task ReadHistory_Should_Return_Empty_When_No_Record()
|
||||
{
|
||||
// Act
|
||||
var result = await Mediator.Send(new ReadHistoryQuery(999));
|
||||
var result = await _host.Mediator.Send(new ReadHistoryQuery(999));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Empty);
|
||||
|
||||
@ -1,41 +1,102 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using EnvelopeGenerator.Application;
|
||||
using EnvelopeGenerator.Application.Receivers.Commands;
|
||||
using EnvelopeGenerator.Infrastructure;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using EnvelopeGenerator.Application.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using EnvelopeGenerator.Application;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace EnvelopeGenerator.Tests.Application;
|
||||
|
||||
public class Mock
|
||||
{
|
||||
[Obsolete("Use MediatR")]
|
||||
public static IHost CreateHost(Action<HostApplicationBuilder>? builderOptions = null, string configPath = "appsettings.json", bool useRealDb = false, params string[] args)
|
||||
public static Host CreateHost() => Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
||||
.ConfigureAppConfiguration((context, config) =>
|
||||
{
|
||||
// add appsettings.json
|
||||
config.SetBasePath(Directory.GetCurrentDirectory());
|
||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
IConfiguration configuration = context.Configuration;
|
||||
|
||||
// add Application and Infrastructure services
|
||||
#pragma warning disable CS0618
|
||||
services.AddEnvelopeGeneratorServices(configuration);
|
||||
services.AddEnvelopeGeneratorInfrastructureServices(
|
||||
(sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"),
|
||||
context.Configuration
|
||||
);
|
||||
#pragma warning restore CS0618
|
||||
})
|
||||
.Build()
|
||||
.ToMock();
|
||||
|
||||
public class Host : IHost
|
||||
{
|
||||
var builder = Host.CreateApplicationBuilder(args.Any() ? args : null);
|
||||
var config = builder.Configuration;
|
||||
builder.Configuration.AddJsonFile(configPath, optional: true, reloadOnChange: true);
|
||||
#region Root
|
||||
private readonly IHost _root;
|
||||
|
||||
builder.Services
|
||||
.AddEnvelopeGeneratorInfrastructureServices((provider, opt) =>
|
||||
public Host(IHost root)
|
||||
{
|
||||
_root = root;
|
||||
}
|
||||
|
||||
public IServiceProvider Services => _root.Services;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_root.Dispose();
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancel = default)
|
||||
{
|
||||
return _root.StartAsync(cancel);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancel = default)
|
||||
{
|
||||
return _root.StopAsync(cancel);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public IMediator Mediator => Services.GetRequiredService<IMediator>();
|
||||
|
||||
#region Sample Receivers
|
||||
public List<(int Id, string EmailAddress)> _sampleReceivers = new();
|
||||
|
||||
public IEnumerable<(int Id, string EmailAddress)> SampleReceivers => _sampleReceivers;
|
||||
|
||||
public async Task AddSampleReceivers()
|
||||
{
|
||||
var mediator = Mediator;
|
||||
var emails = new[]
|
||||
{
|
||||
if (useRealDb)
|
||||
{
|
||||
var connStr = config.GetConnectionString("Default")
|
||||
?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
|
||||
opt.UseSqlServer(connStr);
|
||||
}
|
||||
else
|
||||
opt.UseInMemoryDatabase("MockDB");
|
||||
})
|
||||
.AddEnvelopeGeneratorServices(builder.Configuration)
|
||||
.AddScoped<DocumentStatusService>();
|
||||
|
||||
builderOptions?.Invoke(builder);
|
||||
|
||||
var host = builder.Build();
|
||||
|
||||
return host;
|
||||
"max.mueller@email.de",
|
||||
"anna.schmidt@email.de",
|
||||
"lukas.schneider@email.de",
|
||||
"sophia.fischer@email.de",
|
||||
"jonas.weber@email.de",
|
||||
"lea.hoffmann@email.de",
|
||||
"felix.wagner@email.de",
|
||||
"mia.becker@email.de",
|
||||
"paul.schulz@email.de",
|
||||
"lena.koch@email.de"
|
||||
};
|
||||
foreach (var email in emails)
|
||||
{
|
||||
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
||||
var (Id, _) = await mediator.Send(cmd);
|
||||
_sampleReceivers.Add((Id, email));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static Mock.Host ToMock(this IHost host) => new(host);
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace EnvelopeGenerator.Tests.Application.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class DocumentStatusServiceTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_host = Mock.CreateHost(useRealDb: true);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_host.StopAsync();
|
||||
_host.Dispose();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user