using Barcode.Host.Shared.Models; using Barcode.Host.Shared.Models.Stateless; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Barcode.Host.Tests; [TestClass] public class UnitTestPostService { private static ILogger? _Logger; private static TestContext? _TestContext; private static WebApplicationFactory<Server.Program>? _WebApplicationFactory; [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _WebApplicationFactory = new WebApplicationFactory<Server.Program>(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>(); } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } #if DEBUG [Ignore] #endif [TestMethod] public void GetPostTo() { _Logger?.LogInformation("Starting Web Application"); HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; Server.Models.AppSettings? appSettings = serviceProvider?.GetRequiredService<Server.Models.AppSettings>(); IPostService? postService = serviceProvider?.GetRequiredService<IPostService>(); Assert.IsNotNull(httpClient); Assert.IsNotNull(appSettings); Assert.IsNotNull(postService); Notification? notification = new(KeyPressEvent: null, "Test", appSettings.ToolClass, null); Assert.IsNotNull(notification); _ = postService.Post(appSettings.PostTo, httpClient, notification); _Logger?.LogInformation("{testName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }