This commit is contained in:
Paul Schneider
2021-05-11 20:52:39 +01:00
parent 5bbcf9cb29
commit 512495d10b
3 changed files with 32 additions and 20 deletions

View File

@ -7,38 +7,49 @@ using nuget_host.Data;
using Microsoft.Extensions.Options;
using nuget_host.Entities;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace nuget.host.tests
{
public class UnitTestWebHost
{
const string testingUrl = "http://localhost:5003";
const string testingUrl = "http://localhost:5000";
[Fact]
public async Task TestHaveTestDbContext()
{
IWebHost webhost = BuildWebHost( new string[] { "--urls", testingUrl});
Assert.NotNull(webhost);
webhost.Start();
await webhost.StopAsync();
}
[Fact]
public async Task TestHaveConfig()
{
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Assert.Equal("Development", envVar);
IWebHost webhost = BuildWebHost( new string[] { "--urls", testingUrl });
Assert.NotNull(webhost);
webhost.Start();
await webhost.StopAsync();
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();
}
}