|
- using DotNet.Testcontainers.Builders;
- using DotNet.Testcontainers.Containers;
- using DotNet.Testcontainers.Images;
- using System.Net.Http.Json;
- using TestApiDocker.Models;
-
-
- namespace TestApiDocker
- {
- [TestClass]
- public class MyUnitTest
- {
- private static readonly IContainer _container = new ContainerBuilder()
- .WithImage("ecolocale/my-api:latest")
- .WithPortBinding(9090, 9090)
- .WithCleanUp(true)
- .Build();
-
- [TestInitialize]
- public async Task StartContainer()
- {
- await _container.StartAsync();
-
- }
-
- [TestMethod]
- public async Task ShouldReturnWeatherForecast()
- {
- //Laisser le temps de démarrer que le container se charge
- await Task.Delay(1000);
-
- HttpClient httpClient = new HttpClient();
- Uri requestUri =
- new UriBuilder(
- Uri.UriSchemeHttp,
- _container.Hostname,
- _container.GetMappedPublicPort(9090),
- "WeatherForecast"
- ).Uri;
-
- List<WeatherForecast> weatherForecasts = await httpClient.GetFromJsonAsync<List<WeatherForecast>>(requestUri);
- Assert.AreEqual(5, weatherForecasts.Count());
-
- }
-
- [TestCleanup]
- public async Task DisposeContainer()
- {
- await _container.StopAsync();
-
- }
- }
- }
|