From 5fb9e741dd970d73693c1ee303889af7661a49f6 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Wed, 30 May 2018 22:05:58 -0400 Subject: [PATCH] Updated to 2.1 RTM And updated functional tests to use CustomWebApplicationFactory. --- src/Infrastructure/Infrastructure.csproj | 8 +-- src/Web/Web.csproj | 6 +- src/WebRazorPages/WebRazorPages.csproj | 2 +- tests/FunctionalTests/FunctionalTests.csproj | 2 +- .../Web/Controllers/CatalogControllerIndex.cs | 53 +++------------ .../CustomWebApplicationFactory.cs | 64 +++++++++++++++++++ .../IntegrationTests/IntegrationTests.csproj | 4 +- tests/UnitTests/UnitTests.csproj | 8 +-- 8 files changed, 87 insertions(+), 60 deletions(-) create mode 100644 tests/FunctionalTests/Web/Controllers/CustomWebApplicationFactory.cs diff --git a/src/Infrastructure/Infrastructure.csproj b/src/Infrastructure/Infrastructure.csproj index 8081b93..e051c33 100644 --- a/src/Infrastructure/Infrastructure.csproj +++ b/src/Infrastructure/Infrastructure.csproj @@ -5,10 +5,10 @@ - - - - + + + + diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index d91d29e..19d0d3f 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -6,9 +6,9 @@ - - - + + + diff --git a/src/WebRazorPages/WebRazorPages.csproj b/src/WebRazorPages/WebRazorPages.csproj index 7fea95c..8219d7b 100644 --- a/src/WebRazorPages/WebRazorPages.csproj +++ b/src/WebRazorPages/WebRazorPages.csproj @@ -7,7 +7,7 @@ 231ddc1b-6787-4704-a0c0-18df6a022660 - + diff --git a/tests/FunctionalTests/FunctionalTests.csproj b/tests/FunctionalTests/FunctionalTests.csproj index 7b5df4f..6bb321f 100644 --- a/tests/FunctionalTests/FunctionalTests.csproj +++ b/tests/FunctionalTests/FunctionalTests.csproj @@ -5,7 +5,7 @@ - + diff --git a/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs b/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs index 42963dd..47d210e 100644 --- a/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs +++ b/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs @@ -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> -{ -public CatalogControllerIndex(WebApplicationFactory 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> { - var services = scope.ServiceProvider; - var loggerFactory = services.GetRequiredService(); - try + public CatalogControllerIndex(CustomWebApplicationFactory factory) { - var catalogContext = services.GetRequiredService(); - CatalogContextSeed.SeedAsync(catalogContext, loggerFactory) - .Wait(); - - var userManager = services.GetRequiredService>(); - AppIdentityDbContextSeed.SeedAsync(userManager).Wait(); + Client = factory.CreateClient(new WebApplicationFactoryClientOptions + { + AllowAutoRedirect = false + }); } - catch (Exception ex) - { - var logger = loggerFactory.CreateLogger(); - 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() diff --git a/tests/FunctionalTests/Web/Controllers/CustomWebApplicationFactory.cs b/tests/FunctionalTests/Web/Controllers/CustomWebApplicationFactory.cs new file mode 100644 index 0000000..f0c80e6 --- /dev/null +++ b/tests/FunctionalTests/Web/Controllers/CustomWebApplicationFactory.cs @@ -0,0 +1,64 @@ +using Infrastructure.Data; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.eShopWeb; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; +using Microsoft.EntityFrameworkCore; + +namespace FunctionalTests.Web.Controllers +{ + public class CustomWebApplicationFactory + : WebApplicationFactory + { + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseEnvironment("Testing"); + builder.ConfigureServices(services => + { + // Create a new service provider. + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase() + .BuildServiceProvider(); + + // Add a database context (ApplicationDbContext) using an in-memory + // database for testing. + services.AddDbContext(options => + { + options.UseInMemoryDatabase("InMemoryDbForTesting"); + options.UseInternalServiceProvider(serviceProvider); + }); + + // Build the service provider. + var sp = services.BuildServiceProvider(); + + // Create a scope to obtain a reference to the database + // context (ApplicationDbContext). + using (var scope = sp.CreateScope()) + { + var scopedServices = scope.ServiceProvider; + var db = scopedServices.GetRequiredService(); + var loggerFactory = scopedServices.GetRequiredService(); + + var logger = scopedServices + .GetRequiredService>>(); + + // Ensure the database is created. + db.Database.EnsureCreated(); + + try + { + // Seed the database with test data. + CatalogContextSeed.SeedAsync(db, loggerFactory).Wait(); + } + catch (Exception ex) + { + logger.LogError(ex, $"An error occurred seeding the " + + "database with test messages. Error: {ex.Message}"); + } + } + }); + } + } +} diff --git a/tests/IntegrationTests/IntegrationTests.csproj b/tests/IntegrationTests/IntegrationTests.csproj index 64d663b..1e98e77 100644 --- a/tests/IntegrationTests/IntegrationTests.csproj +++ b/tests/IntegrationTests/IntegrationTests.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/tests/UnitTests/UnitTests.csproj b/tests/UnitTests/UnitTests.csproj index 2ab180e..d316fcf 100644 --- a/tests/UnitTests/UnitTests.csproj +++ b/tests/UnitTests/UnitTests.csproj @@ -5,12 +5,12 @@ - + - - - + + +