isn/test/isnd.tests/UnitTestWebHost.cs

58 lines
1.6 KiB
C#
Raw Normal View History

2021-05-09 13:45:55 +01:00
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Xunit;
2021-08-15 19:09:01 +01:00
using isnd.Data;
2021-05-11 20:52:39 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2021-09-08 01:55:00 +01:00
using Microsoft.Extensions.Options;
2022-04-09 19:53:26 +01:00
using System.Collections.Generic;
2021-09-08 01:55:00 +01:00
2021-08-13 18:55:25 +01:00
namespace isnd.host.tests
2021-05-09 13:45:55 +01:00
{
public class UnitTestWebHost
{
[Fact]
2021-05-11 20:52:39 +01:00
2021-07-05 21:24:37 +01:00
public void TestHaveTestDbContextAndMigrate()
2021-05-09 13:45:55 +01:00
{
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
2021-09-08 01:55:00 +01:00
IWebHost webhost = BuildWebHost(new string[0]);
2021-05-11 20:52:39 +01:00
using (var serviceScope = webhost.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
2021-09-08 01:55:00 +01:00
var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
2021-05-11 20:52:39 +01:00
var myDependency = services.GetRequiredService<ApplicationDbContext>();
myDependency.Database.Migrate();
}
catch (Exception ex)
{
2021-09-08 01:55:00 +01:00
throw new Exception("Failed " + envVar, ex);
2021-05-11 20:52:39 +01:00
}
}
2021-05-09 13:45:55 +01:00
}
2022-04-09 19:53:26 +01:00
2021-05-09 13:45:55 +01:00
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
2021-05-11 20:52:39 +01:00
2022-04-09 19:53:26 +01:00
private class Butts: List<Butt>
{
public bool Im12 { get; set; }
}
private class Butt
{
public bool Im12 { get; set; }
}
2021-05-09 13:45:55 +01:00
}
}