66 lines
2.7 KiB
C#
66 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Serilog;
|
|
using System.Net;
|
|
|
|
namespace Expose.MyIT.Tests;
|
|
|
|
[TestClass]
|
|
public class UnitTestAppSettingsController
|
|
{
|
|
|
|
private static ILogger? _Logger;
|
|
private static string? _ControllerName;
|
|
private static TestContext? _TestContext;
|
|
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
|
|
|
|
[ClassInitialize]
|
|
public static void ClassInitAsync(TestContext testContext)
|
|
{
|
|
_TestContext = testContext;
|
|
_Logger = Log.ForContext<UnitTestAppSettingsController>();
|
|
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
|
|
_ControllerName = nameof(Server.Controllers.AppSettingsController)[..^10];
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestControllerName()
|
|
{
|
|
if (_Logger is null)
|
|
throw new NullReferenceException(nameof(_Logger));
|
|
if (_ControllerName is null)
|
|
throw new NullReferenceException(nameof(_ControllerName));
|
|
if (_WebApplicationFactory is null)
|
|
throw new NullReferenceException(nameof(_WebApplicationFactory));
|
|
_Logger.Information("Starting Web Application");
|
|
Assert.AreEqual(Shared.Models.Stateless.Methods.IAppSettingsController.GetRouteName(), _ControllerName);
|
|
_Logger.Information($"{_TestContext?.TestName} completed");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Get_ShouldReturn()
|
|
{
|
|
if (_Logger is null)
|
|
throw new NullReferenceException(nameof(_Logger));
|
|
if (_ControllerName is null)
|
|
throw new NullReferenceException(nameof(_ControllerName));
|
|
if (_WebApplicationFactory is null)
|
|
throw new NullReferenceException(nameof(_WebApplicationFactory));
|
|
HttpClient httpClient = _WebApplicationFactory.CreateClient();
|
|
_Logger.Information("Starting Web Application");
|
|
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
|
Server.Models.AppSettings appSettings = serviceProvider.GetRequiredService<Server.Models.AppSettings>();
|
|
Assert.IsNotNull(appSettings);
|
|
if (appSettings.IsDevelopment)
|
|
{
|
|
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.IsTrue(result.Contains(appSettings.Company));
|
|
httpClient.Dispose();
|
|
}
|
|
_Logger.Information($"{_TestContext?.TestName} completed");
|
|
}
|
|
|
|
} |