Updated to 2.1 RTM

And updated functional tests to use CustomWebApplicationFactory.
This commit is contained in:
Steve Smith
2018-05-30 22:05:58 -04:00
parent 074bdb2a66
commit 5fb9e741dd
8 changed files with 87 additions and 60 deletions

View File

@@ -1,59 +1,22 @@
using Infrastructure.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Infrastructure.Identity;
using Microsoft.AspNetCore.Identity;
using System;
namespace FunctionalTests.Web.Controllers
{
public class CatalogControllerIndex : IClassFixture<WebApplicationFactory<Startup>>
{
public CatalogControllerIndex(WebApplicationFactory<Startup> fixture)
{
var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder);
Client = factory.CreateClient();
var host = factory.Server?.Host;
SeedData(host);
}
private void SeedData(IWebHost host)
{
if(host == null) { throw new ArgumentNullException("host"); }
using (var scope = host.Services.CreateScope())
public class CatalogControllerIndex : IClassFixture<CustomWebApplicationFactory<Startup>>
{
var services = scope.ServiceProvider;
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
try
public CatalogControllerIndex(CustomWebApplicationFactory<Startup> factory)
{
var catalogContext = services.GetRequiredService<CatalogContext>();
CatalogContextSeed.SeedAsync(catalogContext, loggerFactory)
.Wait();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
catch (Exception ex)
{
var logger = loggerFactory.CreateLogger<CatalogControllerIndex>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
}
private static void ConfigureWebHostBuilder(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
}
public HttpClient Client { get; }
public HttpClient Client { get; }
[Fact]
public async Task ReturnsHomePageWithProductListing()