Taking .NET Core to Production with Tests

Creating the unit test project

> dotnet new xunit --name PathosTest
<ItemGroup>
<ProjectReference Include="..\Pathos\Pathos.csproj" /> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" /> </ItemGroup>

Unit Tests

Creating the first unit test

using Xunit;using Pathos.Controllers;
using Microsoft.AspNetCore.Mvc;
namespace PathosTests.Controllers {
public class HealthControllerTest {
[Fact]
public void TestGetHealth() {
var subject = new HealthController(null);
var actual = subject.Get() as OkObjectResult;
Assert.Equal(actual.Value, "healthy");
}
}
}

Running Tests

Creating Integration Tests

Creating the First Integration Test

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" /> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pathos\Pathos.csproj" /> </ItemGroup>

Getting the test client

using Microsoft.AspNetCore.Mvc.Testing;public class HealthControllerIntegrationTests :
IClassFixture<WebApplicationFactory<Pathos.Startup>> {
private readonly WebApplicationFactory<Pathos.Startup> _factory;
public HealthControllerIntegrationTests(
WebApplicationFactory<Pathos.Startup> factory
) {
this._factory = factory;
}
}
[Fact]
public async void TestGetHealth()
{
var subject = this._factory.CreateClient();
var response = await subject.GetAsync("/api/health");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Advanced Configuration of Test Client

public class PathosDbApplicationFactory<TStartup> :
WebApplicationFactory<TStartup> where tStartup: class
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
var dbOptions = new DbContextOptionsBuilder<PathosContext>();
dbOptions.UseSqlite("DataSource=PathosTests.db");
using (var context = new PathosContext(dbOptions.Options)) {
context.Database.Migrate();
}
builder.ConfigureServices(services => {
services.AddDbContext<PathosContext>(
options => options.UseSqlite("DataSource=PathosTests.db")
);
});
}
public class HealthControllerIntegrationTest :
IClassFixture<PathosDbApplicationFactory<Pathos.Startup>>
{
private readonly PathosDbApplicationFactory<Pathos.Startup> _factory;
public HealthControllerIntegrationTest(
PathosDbApplicationFactory<Pathos.Startup> factory
) {
this._factory = factory;
}
[Fact]
public async void TestGetHealth()
{
// Arrange
var subject = this._factory.CreateClient();
// Act
var response = await subject.GetAsync("/api/health/db");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Swift Table View with Calendar Section Headers

Redirect WordPress Plugins To Download Today

Why I Chose SalesForce Development Over a Web Dev Job

Build a web analytics dashboard in less than 10 minutes

Platform lesson #10: One ecosystem platform stakeholder at a time

Convert object-oriented data to Nosql Dynamodb — 101

Add Health Checks to a DOTVVM/ASP.NET Core Application and Build a Dashboard to Monitor Services

How to use bottom navigation bar in flutter?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Derek Drummond

Derek Drummond

More from Medium

What is CQRS (Command Query Responsibility Segregation) Pattern?

Clean Variables: Volume I

Pass Through Data Over IServiceProvider.CreateScope()

John Doe, The Super Developer — A object-oriented short story of a developer