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.Dto;
|
||||||
using EnvelopeGenerator.Application.Envelopes.Queries;
|
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using System.ComponentModel.DataAnnotations;
|
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.Histories.Queries;
|
||||||
using EnvelopeGenerator.Application.Receivers.Commands;
|
|
||||||
using EnvelopeGenerator.Domain;
|
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;
|
namespace EnvelopeGenerator.Tests.Application;
|
||||||
|
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class HistoryTests
|
public class HistoryTests
|
||||||
{
|
{
|
||||||
private IHost _host;
|
private Mock.Host _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)];
|
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public async Task Setup()
|
public async Task Setup()
|
||||||
{
|
{
|
||||||
_host = Host.CreateDefaultBuilder()
|
_host = Mock.CreateHost();
|
||||||
.ConfigureAppConfiguration((context, config) =>
|
await _host.AddSampleReceivers();
|
||||||
{
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
@ -94,14 +35,14 @@ public class HistoryTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var id = await Mediator.Send(createCmd);
|
var id = await _host.Mediator.Send(createCmd);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(id, Is.Not.Null);
|
Assert.That(id, Is.Not.Null);
|
||||||
|
|
||||||
// ReadHistory query
|
// ReadHistory query
|
||||||
var query = new ReadHistoryQuery(1);
|
var query = new ReadHistoryQuery(1);
|
||||||
var result = await Mediator.Send(query);
|
var result = await _host.Mediator.Send(query);
|
||||||
|
|
||||||
Assert.That(result, Is.Not.Empty);
|
Assert.That(result, Is.Not.Empty);
|
||||||
}
|
}
|
||||||
@ -124,11 +65,11 @@ public class HistoryTests
|
|||||||
Status = Constants.EnvelopeStatus.EnvelopePartlySigned
|
Status = Constants.EnvelopeStatus.EnvelopePartlySigned
|
||||||
};
|
};
|
||||||
|
|
||||||
await Mediator.Send(createCmd1);
|
await _host.Mediator.Send(createCmd1);
|
||||||
await Mediator.Send(createCmd2);
|
await _host.Mediator.Send(createCmd2);
|
||||||
|
|
||||||
// Act
|
// 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
|
||||||
Assert.That(result, Has.Exactly(1).Items);
|
Assert.That(result, Has.Exactly(1).Items);
|
||||||
@ -140,7 +81,7 @@ public class HistoryTests
|
|||||||
public async Task ReadHistory_Should_Return_Empty_When_No_Record()
|
public async Task ReadHistory_Should_Return_Empty_When_No_Record()
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var result = await Mediator.Send(new ReadHistoryQuery(999));
|
var result = await _host.Mediator.Send(new ReadHistoryQuery(999));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.Empty);
|
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 EnvelopeGenerator.Infrastructure;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using EnvelopeGenerator.Application.Services;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using EnvelopeGenerator.Application;
|
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Tests.Application;
|
namespace EnvelopeGenerator.Tests.Application;
|
||||||
|
|
||||||
public class Mock
|
public class Mock
|
||||||
{
|
{
|
||||||
[Obsolete("Use MediatR")]
|
public static Host CreateHost() => Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
||||||
public static IHost CreateHost(Action<HostApplicationBuilder>? builderOptions = null, string configPath = "appsettings.json", bool useRealDb = false, params string[] args)
|
.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);
|
#region Root
|
||||||
var config = builder.Configuration;
|
private readonly IHost _root;
|
||||||
builder.Configuration.AddJsonFile(configPath, optional: true, reloadOnChange: true);
|
|
||||||
|
|
||||||
builder.Services
|
public Host(IHost root)
|
||||||
.AddEnvelopeGeneratorInfrastructureServices((provider, opt) =>
|
{
|
||||||
|
_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)
|
"max.mueller@email.de",
|
||||||
{
|
"anna.schmidt@email.de",
|
||||||
var connStr = config.GetConnectionString("Default")
|
"lukas.schneider@email.de",
|
||||||
?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
|
"sophia.fischer@email.de",
|
||||||
opt.UseSqlServer(connStr);
|
"jonas.weber@email.de",
|
||||||
}
|
"lea.hoffmann@email.de",
|
||||||
else
|
"felix.wagner@email.de",
|
||||||
opt.UseInMemoryDatabase("MockDB");
|
"mia.becker@email.de",
|
||||||
})
|
"paul.schulz@email.de",
|
||||||
.AddEnvelopeGeneratorServices(builder.Configuration)
|
"lena.koch@email.de"
|
||||||
.AddScoped<DocumentStatusService>();
|
};
|
||||||
|
foreach (var email in emails)
|
||||||
builderOptions?.Invoke(builder);
|
{
|
||||||
|
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
||||||
var host = builder.Build();
|
var (Id, _) = await mediator.Send(cmd);
|
||||||
|
_sampleReceivers.Add((Id, email));
|
||||||
return host;
|
}
|
||||||
|
}
|
||||||
|
#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