Overview
FullStackHero includes comprehensive testing strategies to ensure code quality and architectural compliance:- Architecture Tests - Enforce architectural rules and conventions
- Unit Tests - Test individual handlers, validators, and services
- Integration Tests - Test features end-to-end
Test Project Structure
Architecture Tests
Architecture tests use NetArchTest to enforce design rules and conventions.Feature Architecture Tests
Ensure features don’t depend on newer API versions:FeatureArchitectureTests.cs
Handler-Validator Pairing Tests
Ensure every command has a corresponding validator:HandlerValidatorPairingTests.cs
Running Architecture Tests
Unit Tests
Unit tests verify individual components in isolation using mocks and test doubles.Handler Unit Tests
Test command/query handlers using NSubstitute for mocking:ChangePasswordCommandHandlerTests.cs
Validator Unit Tests
Test validators using FluentValidation’s test helpers:CreateGroupCommandValidatorTests.cs
Testing Tools
NuGet Packages
Identity.Tests.csproj
Key Libraries
xUnit
Test framework for writing and running tests
Shouldly
Fluent assertion library with readable error messages
NSubstitute
Mocking library for creating test doubles
AutoFixture
Generate test data automatically
Test Patterns
AAA Pattern (Arrange-Act-Assert)
Test Naming Convention
Handle_Should_ReturnGroup_When_ValidIdProvidedValidate_Should_FailValidation_When_NameIsEmptyCreate_Should_ThrowException_When_DuplicateNameExists
Running Tests
Run All Tests
Run Specific Test Project
Run Specific Test
Run with Coverage
CI/CD Integration
Tests run automatically in GitHub Actions:.github/workflows/test.yml
Best Practices
// Good - tests one thing
[Fact]
public void Should_Throw_When_Name_Is_Empty() { }
// Bad - tests multiple things
[Fact]
public void Should_Validate_All_Fields() { }
// Good
[Fact]
public async Task Handle_Should_ThrowNotFoundException_When_GroupDoesNotExist()
// Bad
[Fact]
public async Task Test1()
Next Steps
Creating Features
Build testable features
Validation
Test validators thoroughly
CI/CD
Automate testing in CI/CD
