tests: fix blog host factory to use built in use kestrel call and dump composite host.
Pull Request / Run tests (pull_request) Successful in 2m3s
Pull Request / Run tests (pull_request) Successful in 2m3s
This commit is contained in:
1 parent
4a7c8b31c5
commit
81cc1e4f64
3 files changed
+8
-70
No files matched your search
@@ -2,62 +2,28 @@ namespace Blog.Tests.EndToEnd;
|
|||||||
|
|
||||||
public class BlogHostFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
|
public class BlogHostFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
|
||||||
{
|
{
|
||||||
private IHost? _host;
|
public BlogHostFactory()
|
||||||
|
|
||||||
private void EnsureServer()
|
|
||||||
{
|
{
|
||||||
if (_host is null)
|
UseKestrel(server => server.Listen(IPAddress.Loopback, 0));
|
||||||
{
|
|
||||||
using var _ = CreateDefaultClient();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
_host?.Dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ServerAddress
|
public string ServerAddress
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
EnsureServer();
|
StartServer();
|
||||||
return ClientOptions.BaseAddress.ToString();
|
return ClientOptions.BaseAddress.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override IHost CreateHost(IHostBuilder builder)
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||||
{
|
{
|
||||||
var testHost = builder
|
builder.ConfigureLogging(config => config.ClearProviders());
|
||||||
.ConfigureWebHost(webHostBuilder =>
|
builder.ConfigureTestServices(services =>
|
||||||
{
|
|
||||||
webHostBuilder.ConfigureLogging(config => config.ClearProviders());
|
|
||||||
webHostBuilder.ConfigureTestServices(services =>
|
|
||||||
{
|
{
|
||||||
var postsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestPosts");
|
var postsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TestPosts");
|
||||||
var postServiceOptions = new FilePostServiceOptions() { PostsDirectory = postsDirectory };
|
var postServiceOptions = new FilePostServiceOptions() { PostsDirectory = postsDirectory };
|
||||||
services.AddSingleton(Options.Create(postServiceOptions));
|
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<IServer>();
|
|
||||||
var addresses = server.Features.GetRequiredFeature<IServerAddressesFeature>();
|
|
||||||
|
|
||||||
ClientOptions.BaseAddress = addresses.Addresses
|
|
||||||
.Select(x => new Uri(x))
|
|
||||||
.Last();
|
|
||||||
|
|
||||||
testHost.Start();
|
|
||||||
return testHost;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,12 +3,12 @@ namespace Blog.Tests.EndToEnd;
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public abstract class BlogTest : PageTest
|
public abstract class BlogTest : PageTest
|
||||||
{
|
{
|
||||||
private readonly BlogHostFactory<Program> _factory = new();
|
private static readonly BlogHostFactory<Program> Factory = new();
|
||||||
|
|
||||||
public override BrowserNewContextOptions ContextOptions()
|
public override BrowserNewContextOptions ContextOptions()
|
||||||
{
|
{
|
||||||
var options = base.ContextOptions();
|
var options = base.ContextOptions();
|
||||||
options.BaseURL = _factory.ServerAddress;
|
options.BaseURL = Factory.ServerAddress;
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user