isn/test/isnd.tests/UnitTestWebHost.cs

76 lines
2.5 KiB
C#

3 years ago
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Xunit;
3 years ago
using isnd.Data;
3 years ago
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2 years ago
using System.Collections.Generic;
2 years ago
using System.Threading.Tasks;
using System.Diagnostics;
2 years ago
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using System.Threading;
3 years ago
namespace isnd.host.tests
3 years ago
{
public class UnitTestWebHost
{
[Fact]
public void TestHaveTestDbContextAndMigrate()
3 years ago
{
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
2 years ago
IWebHost webhost = BuildWebHost(new string[0]);
3 years ago
using (var serviceScope = webhost.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
3 years ago
var myDependency = services.GetRequiredService<ApplicationDbContext>();
myDependency.Database.Migrate();
}
catch (Exception ex)
{
throw new Exception("Failed " + envVar, ex);
3 years ago
}
}
3 years ago
}
2 years ago
3 years ago
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
2 years ago
.UseStartup<Startup>().Build();
3 years ago
2 years ago
[Fact]
2 years ago
2 years ago
public async Task NugetInstallsTest()
2 years ago
{
2 years ago
ProcessStartInfo psi = new ProcessStartInfo("nuget");
psi.ArgumentList.Add("install");
2 years ago
psi.ArgumentList.Add("gitversion");
psi.ArgumentList.Add("-PreRelease");
psi.ArgumentList.Add("-Source");
psi.ArgumentList.Add("http://localhost:5000");
2 years ago
Process p = Process.Start(psi);
p.WaitForExit();
2 years ago
Assert.True(p.ExitCode == 0, "nuget install failed!");
2 years ago
}
2 years ago
void PrintAddresses(IServiceProvider services)
{
Console.WriteLine("Checking addresses...");
var server = services.GetRequiredService<IServer>();
var addressFeature = server.Features.Get<IServerAddressesFeature>();
foreach (var address in addressFeature.Addresses)
{
Console.WriteLine("Listing on address: " + address);
}
}
3 years ago
}
}