liveBook · Manning

Chapter 36: Testing ASP.NET Core applications

What you will learn in chapter 36 of ASP.NET Core in Action, 3e

This chapter introduces unit and integration testing strategies and tools for ASP.NET Core-specific components, complete applications, and EF Core-dependent services.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces unit and integration testing strategies and tools for ASP.NET Core-specific components, complete applications, and EF Core-dependent services.

Key terms: unit tests, integration tests, custom middleware, API controllers, minimal API endpoints, middleware pipeline, Test Host package, WebApplicationFactory, SQLite database provider, Entity Framework Core

36.1 Unit testing custom middleware

This section shows how to unit test a custom middleware component in isolation for both pass-through and response-producing requests.

Key terms: StatusMiddleware, Invoke, RequestDelegate, /ping, pong, DefaultHttpContext, HttpContext, Assert, Response.Body, Stream.Null

36.2 Unit testing API controllers and minimal API endpoints

This section evaluates unit testing controllers and minimal API endpoints in isolation, showing how to do it and why thin endpoints plus integration tests are often preferable.

Key terms: controllers, Razor Pages, PageModels, business logic services, resource-based authorization, orchestrators, unit test, action method, ActionResult<T>, MVC formatter infrastructure

36.3 Integration testing: Testing your whole app in-memory

This introduction defines integration testing in contrast to unit testing and outlines testing ASP.NET Core HTTP request handling in memory with TestServer and WebApplicationFactory.

Key terms: integration tests, unit tests, StatusMiddleware, API controller, external infrastructure, Microsoft.AspNetCore.TestHost, TestServer, WebApplicationFactory, middleware pipeline, third-party APIs

36.3.1 Creating a TestServer using the Test Host package

This section shows how to create an in-memory integration test host with the Test Host package and send requests to it through an HttpClient.

Key terms: Test Host, Microsoft.AspNetCore.TestHost, TestServer, in-memory requests, Kestrel, IHost, HostBuilder, ConfigureWebHost, Configure, UseTestServer

36.3.2 Testing your application with WebApplicationFactory

This section shows how to use WebApplicationFactory to run and test a real ASP.NET Core application in memory with its normal configuration, services, and middleware pipeline.

Key terms: WebApplicationFactory, TestServer, Microsoft.AspNetCore.Mvc.Testing, dependency injection, middleware pipeline, WebApplicationFactory<T>, IClassFixture<TFixture>, Program, public partial class Program, HttpClient

36.3.3 Replacing dependencies in WebApplicationFactory

This section shows how to replace an application's real dependencies with test doubles when using WebApplicationFactory for integration tests.

Key terms: WebApplicationFactory, Development, in-memory, dotnet run, CurrencyConverter, ICurrencyConverter, StubCurrencyConverter, IHttpClientFactory, testable, WithWebHostBuilder

36.3.4 Reducing duplication by creating a custom WebApplicationFactory

This section shows how a custom WebApplicationFactory centralizes recurring integration-test service replacements while preserving optional per-test overrides.

Key terms: WebApplicationFactory, ConfigureWebHost, test services, IClassFixture, WithWebHostBuilder, per-test configuration

36.4 Isolating the database with an in-memory EF Core provider

This section shows how to test EF Core-dependent code quickly and in isolation by using an in-memory SQLite database instead of a real database.

Key terms: DbContext, unit tests, in-memory database provider, isolated tests, Microsoft.EntityFrameworkCore.InMemory, Microsoft.EntityFrameworkCore.Sqlite, SQLite, relational database, in-memory mode, constraints

36.5 Summary

This summary recaps unit and integration testing techniques for ASP.NET Core components, whole applications, and EF Core database code.

Concepts explained in this chapter

← Chapter 35: Testing applications with xUnit

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.