using Microsoft.AspNetCore.Mvc.Testing; using Serilog; using System.Net; namespace Expose.MyIT.Tests; [TestClass] public class UnitTestWeatherForecastController { #pragma warning disable CS8618 private static ILogger _Logger; private static string _ControllerName; private static TestContext _TestContext; private static WebApplicationFactory _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _Logger = Log.ForContext(); _WebApplicationFactory = new WebApplicationFactory(); _ControllerName = nameof(Server.Controllers.WeatherForecastController)[..^10]; } [TestMethod] public void TestControllerName() { _Logger.Information("Starting Web Application"); Assert.AreEqual(Shared.Models.Stateless.IWeatherForecastController.GetRouteName(), _ControllerName); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public async Task GetReactors_ShouldReturnAllWeatherForecast() { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); string result = await httpResponseMessage.Content.ReadAsStringAsync(); Assert.IsNotNull(result); httpClient.Dispose(); _Logger.Information($"{_TestContext?.TestName} completed"); } }