You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 line
1.5KB

  1. using DotNet.Testcontainers.Builders;
  2. using DotNet.Testcontainers.Containers;
  3. using DotNet.Testcontainers.Images;
  4. using System.Net.Http.Json;
  5. using TestApiDocker.Models;
  6. namespace TestApiDocker
  7. {
  8. [TestClass]
  9. public class MyUnitTest
  10. {
  11. private static readonly IContainer _container = new ContainerBuilder()
  12. .WithImage("ecolocale/my-api:latest")
  13. .WithPortBinding(9090, 9090)
  14. .WithCleanUp(true)
  15. .Build();
  16. [TestInitialize]
  17. public async Task StartContainer()
  18. {
  19. await _container.StartAsync();
  20. }
  21. [TestMethod]
  22. public async Task ShouldReturnWeatherForecast()
  23. {
  24. //Laisser le temps de démarrer que le container se charge
  25. await Task.Delay(1000);
  26. HttpClient httpClient = new HttpClient();
  27. Uri requestUri =
  28. new UriBuilder(
  29. Uri.UriSchemeHttp,
  30. _container.Hostname,
  31. _container.GetMappedPublicPort(9090),
  32. "WeatherForecast"
  33. ).Uri;
  34. List<WeatherForecast> weatherForecasts = await httpClient.GetFromJsonAsync<List<WeatherForecast>>(requestUri);
  35. Assert.AreEqual(5, weatherForecasts.Count());
  36. }
  37. [TestCleanup]
  38. public async Task DisposeContainer()
  39. {
  40. await _container.StopAsync();
  41. }
  42. }
  43. }