diff --git a/test/Blog.Tests/EndToEnd/BlogHostFactory.cs b/test/Blog.Tests/EndToEnd/BlogHostFactory.cs index f260fb9..6176643 100644 --- a/test/Blog.Tests/EndToEnd/BlogHostFactory.cs +++ b/test/Blog.Tests/EndToEnd/BlogHostFactory.cs @@ -2,62 +2,28 @@ namespace Blog.Tests.EndToEnd; public class BlogHostFactory : WebApplicationFactory where TProgram : class { - private IHost? _host; - - private void EnsureServer() + public BlogHostFactory() { - if (_host is null) - { - using var _ = CreateDefaultClient(); - } - } - - protected override void Dispose(bool disposing) - { - _host?.Dispose(); + UseKestrel(server => server.Listen(IPAddress.Loopback, 0)); } public string ServerAddress { get { - EnsureServer(); + StartServer(); return ClientOptions.BaseAddress.ToString(); } } - protected override IHost CreateHost(IHostBuilder builder) + protected override void ConfigureWebHost(IWebHostBuilder builder) { - var testHost = builder - .ConfigureWebHost(webHostBuilder => - { - webHostBuilder.ConfigureLogging(config => config.ClearProviders()); - webHostBuilder.ConfigureTestServices(services => - { - var postsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestPosts"); - var postServiceOptions = new FilePostServiceOptions() { PostsDirectory = postsDirectory }; - services.AddSingleton(Options.Create(postServiceOptions)); - }); - }) - .Build(); - - builder.ConfigureWebHost( - webHostBuilder => webHostBuilder.UseKestrel( - o => o.Listen(IPAddress.Loopback, 0) - ) - ); - - _host = builder.Build(); - _host.Start(); - - var server = _host.Services.GetRequiredService(); - var addresses = server.Features.GetRequiredFeature(); - - ClientOptions.BaseAddress = addresses.Addresses - .Select(x => new Uri(x)) - .Last(); - - testHost.Start(); - return testHost; + builder.ConfigureLogging(config => config.ClearProviders()); + builder.ConfigureTestServices(services => + { + var postsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestPosts"); + var postServiceOptions = new FilePostServiceOptions() { PostsDirectory = postsDirectory }; + services.AddSingleton(Options.Create(postServiceOptions)); + }); } } \ No newline at end of file diff --git a/test/Blog.Tests/EndToEnd/BlogTest.cs b/test/Blog.Tests/EndToEnd/BlogTest.cs index 6326d36..c9aca52 100644 --- a/test/Blog.Tests/EndToEnd/BlogTest.cs +++ b/test/Blog.Tests/EndToEnd/BlogTest.cs @@ -3,12 +3,12 @@ namespace Blog.Tests.EndToEnd; [TestFixture] public abstract class BlogTest : PageTest { - private readonly BlogHostFactory _factory = new(); + private static readonly BlogHostFactory Factory = new(); public override BrowserNewContextOptions ContextOptions() { var options = base.ContextOptions(); - options.BaseURL = _factory.ServerAddress; + options.BaseURL = Factory.ServerAddress; return options; } @@ -30,10 +30,10 @@ public abstract class BlogTest : PageTest await Context.Tracing.StopAsync(new() { Path = Path.Combine( - TestContext.CurrentContext.WorkDirectory, - "playwright-traces", - $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip" - ) + TestContext.CurrentContext.WorkDirectory, + "playwright-traces", + $"{TestContext.CurrentContext.Test.ClassName}.{TestContext.CurrentContext.Test.Name}.zip" + ) }); } } \ No newline at end of file diff --git a/test/Blog.Tests/EndToEnd/CompositeHost.cs b/test/Blog.Tests/EndToEnd/CompositeHost.cs deleted file mode 100644 index 7989fb7..0000000 --- a/test/Blog.Tests/EndToEnd/CompositeHost.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace Blog.Tests.EndToEnd; - -public class CompositeHost(IHost testHost, IHost kestrelHost) : IHost -{ - private readonly IHost _testHost = testHost; - private readonly IHost _kestrelHost = kestrelHost; - - public IServiceProvider Services => _testHost.Services; - - public void Dispose() - { - _testHost.Dispose(); - _kestrelHost.Dispose(); - GC.SuppressFinalize(this); - } - - public async Task StartAsync(CancellationToken cancellationToken = default) - { - await _testHost.StartAsync(cancellationToken); - await _kestrelHost.StartAsync(cancellationToken); - } - - public async Task StopAsync(CancellationToken cancellationToken = default) - { - await _testHost.StopAsync(cancellationToken); - await _kestrelHost.StopAsync(cancellationToken); - } -} \ No newline at end of file