Tests passed using Mock
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using Serilog;
|
||||
using System.Net;
|
||||
|
||||
namespace OI.Metrology.Tests;
|
||||
@ -12,10 +12,10 @@ public class UnitTestClientSettingsController
|
||||
|
||||
#pragma warning disable CS8618
|
||||
|
||||
private static ILogger _Logger;
|
||||
private static ILogger? _Logger;
|
||||
private static string _ControllerName;
|
||||
private static TestContext _TestContext;
|
||||
private static WebApplicationFactory<Server.Program> _WebApplicationFactory;
|
||||
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
|
||||
|
||||
#pragma warning restore
|
||||
|
||||
@ -23,38 +23,40 @@ public class UnitTestClientSettingsController
|
||||
public static void ClassInitAsync(TestContext testContext)
|
||||
{
|
||||
_TestContext = testContext;
|
||||
_Logger = Log.ForContext<UnitTestClientSettingsController>();
|
||||
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
|
||||
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
||||
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
|
||||
_ControllerName = nameof(Server.ApiControllers.ClientSettingsController)[..^10];
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestControllerName()
|
||||
{
|
||||
_Logger.Information("Starting Web Application");
|
||||
_Logger?.LogInformation("Starting Web Application");
|
||||
Assert.AreEqual(IClientSettingsController<object>.GetRouteName(), _ControllerName);
|
||||
_Logger.Information($"{_TestContext?.TestName} completed");
|
||||
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetClientSettings()
|
||||
{
|
||||
_Logger.Information("Starting Web Application");
|
||||
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
||||
IClientSettingsRepository clientSettingsRepository = serviceProvider.GetRequiredService<IClientSettingsRepository>();
|
||||
_Logger?.LogInformation("Starting Web Application");
|
||||
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
||||
IClientSettingsRepository? clientSettingsRepository = serviceProvider?.GetRequiredService<IClientSettingsRepository>();
|
||||
#if DEBUG
|
||||
List<string> clientSettings = clientSettingsRepository.GetClientSettings(null);
|
||||
List<string>? clientSettings = clientSettingsRepository?.GetClientSettings(null);
|
||||
Assert.IsTrue(clientSettings is not null);
|
||||
#endif
|
||||
_Logger.Information($"{_TestContext?.TestName} completed");
|
||||
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetClientSettingsApi()
|
||||
{
|
||||
HttpClient httpClient = _WebApplicationFactory.CreateClient();
|
||||
_Logger.Information("Starting Web Application");
|
||||
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
||||
_Logger?.LogInformation("Starting Web Application");
|
||||
#if DEBUG
|
||||
Assert.IsTrue(httpClient is not null);
|
||||
string actionName = nameof(IClientSettingsController<object>.Action.Client);
|
||||
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
|
||||
Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode);
|
||||
@ -64,25 +66,26 @@ public class UnitTestClientSettingsController
|
||||
Assert.IsNotNull(json);
|
||||
Assert.IsTrue(json != "[]");
|
||||
#endif
|
||||
_Logger.Information($"{_TestContext?.TestName} completed");
|
||||
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetIpAddress()
|
||||
{
|
||||
_Logger.Information("Starting Web Application");
|
||||
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
||||
IClientSettingsRepository clientSettingsRepository = serviceProvider.GetRequiredService<IClientSettingsRepository>();
|
||||
string? ipAddress = clientSettingsRepository.GetIpAddress(null);
|
||||
_Logger?.LogInformation("Starting Web Application");
|
||||
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
||||
IClientSettingsRepository? clientSettingsRepository = serviceProvider?.GetRequiredService<IClientSettingsRepository>();
|
||||
string? ipAddress = clientSettingsRepository?.GetIpAddress(null);
|
||||
Assert.IsTrue(ipAddress is not null);
|
||||
_Logger.Information($"{_TestContext?.TestName} completed");
|
||||
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetIpAddressApi()
|
||||
{
|
||||
HttpClient httpClient = _WebApplicationFactory.CreateClient();
|
||||
_Logger.Information("Starting Web Application");
|
||||
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
||||
_Logger?.LogInformation("Starting Web Application");
|
||||
Assert.IsTrue(httpClient is not null);
|
||||
string actionName = nameof(IClientSettingsController<object>.Action.IP);
|
||||
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
|
||||
Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode);
|
||||
@ -90,7 +93,7 @@ public class UnitTestClientSettingsController
|
||||
string json = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetIpAddress)}.json"), json);
|
||||
Assert.IsNotNull(json);
|
||||
_Logger.Information($"{_TestContext?.TestName} completed");
|
||||
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user