48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore;
|
|
using Xunit;
|
|
using isnd.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
namespace isnd.host.tests
|
|
{
|
|
public class UnitTestWebHost
|
|
{
|
|
const string testingUrl = "http://localhost:5000";
|
|
|
|
|
|
[Fact]
|
|
public void TestHaveTestDbContextAndMigrate()
|
|
{
|
|
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
|
Assert.Equal("Development", envVar);
|
|
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.");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static IWebHost BuildWebHost(string[] args) =>
|
|
WebHost.CreateDefaultBuilder(args)
|
|
.UseStartup<Startup>()
|
|
.Build();
|
|
|
|
}
|
|
}
|