isn/test/isnd.tests/UnitTestWebHost.cs

48 lines
1.5 KiB
C#
Raw Normal View History

2021-05-09 13:45:55 +01:00
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
2021-07-05 12:55:52 +01:00
using isn;
2021-05-09 13:45:55 +01:00
using Xunit;
2021-07-05 12:55:52 +01:00
using isn.Data;
2021-05-11 20:52:39 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2021-08-13 18:55:25 +01:00
namespace isnd.host.tests
2021-05-09 13:45:55 +01:00
{
public class UnitTestWebHost
{
2021-05-11 20:52:39 +01:00
const string testingUrl = "http://localhost:5000";
2021-05-09 13:45:55 +01:00
[Fact]
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");
Assert.Equal("Development", envVar);
2021-05-11 20:52:39 +01:00
IWebHost webhost = BuildWebHost(new string[] { "--urls", testingUrl });
using (var serviceScope = webhost.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var myDependency = services.GetRequiredService<ApplicationDbContext>();
myDependency.Database.Migrate();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred.");
}
}
2021-05-09 13:45:55 +01:00
}
2021-08-13 18:55:25 +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
2021-05-09 13:45:55 +01:00
}
}