Last active
June 4, 2019 03:41
-
-
Save jongalloway/7ca5fb724cb4ed887506249e2812d9c9 to your computer and use it in GitHub Desktop.
ASP.NET Core 2.1 Functional Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc.Testing; | |
using System; | |
using System.Threading.Tasks; | |
using Xunit; | |
namespace WebApp.Tests | |
{ | |
public class BasicTests | |
: IClassFixture<WebApplicationFactory<Startup>> | |
{ | |
private readonly WebApplicationFactory<Startup> _factory; | |
public BasicTests(WebApplicationFactory<Startup> factory) | |
{ | |
_factory = factory; | |
} | |
[Theory] | |
[InlineData("/")] | |
[InlineData("/Index")] | |
[InlineData("/About")] | |
[InlineData("/Privacy")] | |
[InlineData("/Contact")] | |
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url) | |
{ | |
// Arrange | |
var client = _factory.CreateClient(); | |
// Act | |
var response = await client.GetAsync(url); | |
// Assert | |
response.EnsureSuccessStatusCode(); // Status Code 200-299 | |
Assert.Equal("text/html; charset=utf-8", | |
response.Content.Headers.ContentType.ToString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public async Task Get_HealthCheckReturnsHealthy() | |
{ | |
// Arrange | |
var client = _factory.CreateClient(); | |
// Act | |
var response = await client.GetAsync("/health"); | |
// Assert | |
response.EnsureSuccessStatusCode(); // Status Code 200-299 | |
Assert.Equal("Healthy", | |
response.Content.ReadAsStringAsync().Result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
<IsPackable>false</IsPackable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.2" /> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> | |
<PackageReference Include="xunit" Version="2.3.1" /> | |
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | |
</ItemGroup> | |
<ItemGroup> | |
<ProjectReference Include="..\WebApp\WebApp.csproj" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment