9. Testing ASP.NET Core
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
The Testing Pyramid
Test Types Overview
Test Type
What It Tests
Speed
Complexity
Quantity
Popular Testing Frameworks
2. Unit Testing (3 Approaches)
Method 1: Simple Tests (No Framework Setup)
Method 2: Testing with Dependencies (Mocking)
Method 3: Testing with Test Fixtures (Shared Setup)
3. Integration Testing (3 Approaches)
Method 1: WebApplicationFactory (Standard Approach)
Method 2: Custom WebApplicationFactory (Production Approach) ⭐ RECOMMENDED
Method 3: Integration Tests with Authentication
4. Testing Controllers & APIs
Testing Controller Actions Directly
5. Testing with Database (DbContext)
Method 1: In-Memory Database (Quick Tests)
Method 2: SQLite In-Memory (Better for Testing)
Method 3: Real Database with Docker (Integration Tests)
6. Mocking and Test Doubles
Types of Test Doubles
Using Moq - Complete Guide
7. Common Testing Patterns
Arrange-Act-Assert (AAA) Pattern
Testing Exceptions
Testing Collections
Data-Driven Tests with Theory
Test Setup and Cleanup
8. Troubleshooting Common Issues
Problem: Tests Pass Individually but Fail Together
Problem: DbContext Tracking Issues
Problem: Async Tests Don't Wait
Problem: Mock Not Working as Expected
Problem: Integration Tests are Slow
9. Best Practices
✅ DO:
❌ DON'T:
Test Organization
Code Coverage Guidelines
PART 2: TECHNICAL REFERENCE
10. Important Interfaces & Classes Reference
xUnit Attributes
Attribute
Purpose
Example
Assert Class (xUnit.Assert)
WebApplicationFactory
Member
Type
Purpose
Mock Class (Moq)
Method
Purpose
Example
IClassFixture Interface
IAsyncLifetime Interface
11. Configuration Deep-Dive
Pattern 1: Inline Configuration (Simple Tests)
Pattern 2: Base Class Setup (Reusable)
Pattern 3: Test Configuration Files (Production-Like)
Configuration Comparison
Pattern
Complexity
Reusability
Maintenance
Production-Like
12. Advanced Testing Topics
Testing Middleware
Testing Background Services
Testing SignalR Hubs
Testing with Time/DateTime
Snapshot Testing
13. Performance Testing Basics
BenchmarkDotNet Setup
Load Testing with NBomber
Summary: Complete Testing Checklist
Last updated