10. Testing (Advanced)
ASP.NET Core Testing - Complete Guide
Practical Guide + Technical Reference
π Table of Contents
Part 1: Practical Guide (Hands-On)
Part 2: Technical Reference (Deep Dive)
PART 1: PRACTICAL GUIDE
1. Testing Fundamentals
Testing Pyramid
Types of Tests in ASP.NET Core
Type
What It Tests
Speed
Cost
Quantity
2. Unit Testing Setup (3 Methods)
Method 1: xUnit Only (Simple & Quick)
Method 2: xUnit + Moq + FluentAssertions (Production) β RECOMMENDED
Method 3: xUnit + NSubstitute + Shouldly (Alternative)
3. Integration Testing Setup (3 Methods)
Method 1: WebApplicationFactory (Standard)
Method 2: Custom WebApplicationFactory (Production) β RECOMMENDED
Method 3: TestServer (Lightweight)
4. Testing Controllers & APIs
Testing GET Endpoints
Testing POST Endpoints
Testing PUT Endpoints
Testing DELETE Endpoints
Testing Controller Actions Directly (Unit Testing)
5. Testing Middleware
Unit Testing Middleware
Integration Testing Middleware
6. Testing with Databases
Method 1: In-Memory Database (Fast, Simple)
Method 2: SQLite In-Memory (Better for Real SQL)
Method 3: Test Containers (Real Database) β Most Realistic
7. Common Testing Patterns
AAA Pattern (Arrange-Act-Assert)
Test Fixtures (Shared Setup)
Testing Async Methods
Testing Exceptions
8. Mocking & Test Doubles
Mock Setup Patterns
Argument Matchers
Mock vs Stub vs Fake
9. Troubleshooting Common Issues
Issue 1: Tests Interfering with Each Other
Issue 2: Async Tests Hanging
Issue 3: Mock Not Being Called
Issue 4: Integration Tests Not Finding Program
Issue 5: Database Context Disposed
10. Best Practices
Naming Conventions
Test Organization
Test Data Builders
DRY Principle (Don't Repeat Yourself)
Test Categories (Tags)
Code Coverage
Best Practices Checklist
PART 2: TECHNICAL REFERENCE
11. Important Testing Interfaces & Classes Reference
IClassFixture
IAsyncLifetime
Member
Return Type
Description
WebApplicationFactory
Member
Type
Description
Mock (Moq Library)
Member
Description
It Class (Moq Argument Matching)
Method
Description
Example
FluentAssertions Extensions
Times (Moq Verification)
Member
Description
TestServer
Member
Type
Description
12. Configuration Deep-Dive
Pattern 1: In-Memory Database (Hardcoded)
Pattern 2: SQLite In-Memory (Code-based)
Pattern 3: Test Containers (Production-like)
Configuration Comparison
Approach
Speed
Accuracy
Complexity
Use Case
13. Advanced Testing Topics
Testing Background Services
Testing SignalR Hubs
Testing gRPC Services
Testing with Time (Fake Clock)
Snapshot Testing
Property-Based Testing
Mutation Testing
14. Performance Testing
Benchmark Testing with BenchmarkDotNet
Load Testing
Response Time Testing
Summary: Complete Testing Checklist
Last updated