From 074bdb2a666abdfcbd50f9f2f8f100b5ac8ee402 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Tue, 29 May 2018 22:20:07 -0400 Subject: [PATCH] Adding functional tests using WebApplicationFactory --- src/Web/Startup.cs | 4 +- tests/FunctionalTests/FunctionalTests.csproj | 8 +-- .../Web/Controllers/CatalogControllerIndex.cs | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index 1ca717b..edd33e2 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -61,9 +61,9 @@ namespace Microsoft.eShopWeb // https://www.microsoft.com/en-us/download/details.aspx?id=54284 c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection")); } - catch (System.Exception ex) + catch (Exception ex) { - var message = ex.Message; + //TODO: log the exception details } }); diff --git a/tests/FunctionalTests/FunctionalTests.csproj b/tests/FunctionalTests/FunctionalTests.csproj index 72aaccb..7b5df4f 100644 --- a/tests/FunctionalTests/FunctionalTests.csproj +++ b/tests/FunctionalTests/FunctionalTests.csproj @@ -5,11 +5,11 @@ + - - - - + + + diff --git a/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs b/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs new file mode 100644 index 0000000..42963dd --- /dev/null +++ b/tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs @@ -0,0 +1,70 @@ +using Infrastructure.Data; +using Microsoft.AspNetCore.Hosting; +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()) + { + var services = scope.ServiceProvider; + var loggerFactory = services.GetRequiredService(); + try + { + var catalogContext = services.GetRequiredService(); + CatalogContextSeed.SeedAsync(catalogContext, loggerFactory) + .Wait(); + + var userManager = services.GetRequiredService>(); + AppIdentityDbContextSeed.SeedAsync(userManager).Wait(); + } + 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; } + + [Fact] + public async Task ReturnsHomePageWithProductListing() + { + // Arrange & Act + var response = await Client.GetAsync("/"); + response.EnsureSuccessStatusCode(); + var stringResponse = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Contains(".NET Bot Black Sweatshirt", stringResponse); + } + } +}