

public class IntegrationTestWebAppFactory : WebApplicationFactory, IAsyncLifetime
{
private readonly IContainer _apiContainer;
public HttpClient Client { get; private set; }
public IntegrationTestWebAppFactory()
{
Console.WriteLine("initiating the factory");
_apiContainer = new ContainerBuilder()
.WithImage("***.azurecr.io/weather.api:v1")
.WithEnvironment("DOCKER_REGISTRY_URL", "***")
.WithEnvironment("DOCKER_REGISTRY_USERNAME", "***")
.WithEnvironment("DOCKER_REGISTRY_PASSWORD", "***")
.WithPortBinding(8080, 80)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(8080)))
.Build();
}
public async Task InitializeAsync()
{
Console.WriteLine("InitializeAsync");
await _apiContainer.StartAsync().ConfigureAwait(false);
Client = new HttpClient
{
BaseAddress = new Uri("http://localhost:8080/")
};
Console.WriteLine("InitializeAsync done");
}
async Task IAsyncLifetime.DisposeAsync()
{
Console.WriteLine("DisposeAsync");
await _apiContainer.DisposeAsync().ConfigureAwait(false);
}
}
public class UnitTest1 : IClassFixture
{
private readonly IntegrationTestWebAppFactory _factory;
public UnitTest1(IntegrationTestWebAppFactory factory)
{
_factory = factory;
}
[Fact]
public async Task Should_Return_Forcast()
{
// Arrange
if(_factory is null)
{
Console.WriteLine("factory is null");
return;
}
var client = _factory.Client;
// Act
var response = await client.GetAsync("http://localhost:8080/WeatherForecast");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(responseString);
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... ck-at-test