Adding functional tests using WebApplicationFactory
This commit is contained in:
@@ -61,9 +61,9 @@ namespace Microsoft.eShopWeb
|
|||||||
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
|
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
|
||||||
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
|
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
|
||||||
}
|
}
|
||||||
catch (System.Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var message = ex.Message;
|
//TODO: log the exception details
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.0-rc1-final" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0-preview-20180510-03" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0-preview-20180510-03" />
|
||||||
<PackageReference Include="xunit" Version="2.4.0-beta.1.build3958" />
|
<PackageReference Include="xunit" Version="2.3.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0-beta.1.build3958" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.0-rc1-final" />
|
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -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<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())
|
||||||
|
{
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var catalogContext = services.GetRequiredService<CatalogContext>();
|
||||||
|
CatalogContextSeed.SeedAsync(catalogContext, loggerFactory)
|
||||||
|
.Wait();
|
||||||
|
|
||||||
|
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||||
|
AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
|
||||||
|
}
|
||||||
|
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; }
|
||||||
|
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user