|
| 1 | +using Common; |
| 2 | +using Microsoft.AspNetCore.Hosting; |
| 3 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using Serilog; |
| 8 | +using Serilog.Sinks.XUnit3; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace IntegrationTests.Setup; |
| 12 | + |
| 13 | +public sealed class TestApplicationFactory(Fixture fixture) : WebApplicationFactory<Program> |
| 14 | +{ |
| 15 | + protected override void ConfigureWebHost(IWebHostBuilder builder) |
| 16 | + { |
| 17 | + if (EnvVarAccessors.UseMocking) |
| 18 | + { |
| 19 | + builder?.UseSetting(EnvVarKeys.MarketClientUrl, $"http://localhost:{fixture.GetPort()}"); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + protected override IHost CreateHost(IHostBuilder builder) |
| 24 | + { |
| 25 | + builder.ConfigureServices(services => |
| 26 | + { |
| 27 | + services.AddSingleton(Options.Create(new XUnit3TestOutputSinkOptions())); |
| 28 | + services.AddSingleton<XUnit3TestOutputSink>(); |
| 29 | + }); |
| 30 | + |
| 31 | + builder.UseSerilog((ctx, sp, config) => |
| 32 | + config.MinimumLevel.Information() |
| 33 | + .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error) |
| 34 | + .MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Error) |
| 35 | + .WriteTo.XUnit3TestOutput(sp.GetRequiredService<XUnit3TestOutputSink>()) |
| 36 | + ); |
| 37 | + |
| 38 | + return base.CreateHost(builder); |
| 39 | + } |
| 40 | + |
| 41 | + public void SetTestOutputHelper(ITestOutputHelper testOutputHelper) => |
| 42 | + Services.GetRequiredService<XUnit3TestOutputSink>().TestOutputHelper = testOutputHelper; |
| 43 | +} |
0 commit comments