Tutorials Clean Architecture & DDD Mastery

Testing Use Cases with Mocks

On this page

Orchestration Testing

Testing the Application Layer requires isolation. We need to verify that the 'Service' coordinates the domain and infrastructure correctly.

1. Mocking the Outside World

When testing a Use Case (like CreateOrderHandler), you mock the IOrderRepository and IEmailService. You check that the handler calls repository.Add() and then unitOfWork.SaveChangesAsync(). This verifies that the 'Recipe' of the use case is correct without needing a real database or a real email server.

2. MediatR Pipeline Testing

If you're using MediatR Behaviors for logging or validation, you can test these in isolation too. This ensures your cross-cutting concerns are working globally without having to repeat their tests for every single handler.

3. Architect Insight

Q: "Moq or NSubstitute?"

Architect Answer: "Both are excellent. **NSubstitute** has a cleaner, more readable syntax (e.g., repo.Received().Add(item)) while **Moq** is the veteran with more advanced features. For Clean Architecture, readability is king, so NSubstitute is often the preferred choice for senior .NET teams today."

Clean Architecture & DDD Mastery
Course syllabus
1. Architectural Patterns The Evolution of Architecture: Monolith to Clean Onion Architecture: Dependency Inversion at the core Clean Architecture: The 'Screaming' architecture Hexagonal Architecture (Ports and Adapters)
2. Domain-Driven Design (DDD) Foundations Ubiquitous Language: Aligning code with business Entities vs Value Objects: Managing identity and state Aggregates & Aggregate Roots: Defining consistency boundaries Bounded Contexts: Handling complexity in large domains
3. Advanced DDD Patterns Domain Services: When logic doesn't fit in an entity Domain Events: Decoupling side effects via events Repositories: Mediating between domain and data Unit of Work: Ensuring atomic transactions
4. Implementing the Clean Layers The Domain Layer: Zero dependencies, pure C# The Application Layer: Orchestrating use cases The Infrastructure Layer: Bridging to the outside world The Presentation Layer: Decoupling the UI from logic
5. Patterns for Data & Logic CQRS (Command Query Responsibility Segregation) MediatR: Implementing the Mediator pattern in .NET Specification Pattern: Encapsulating business rules Policy Pattern: Handling complex authorization rules
6. Enterprise Domain Challenges Handling Persistence Ignorance with EF Core Mapping Layers: AutoMapper vs Manual Mapping Validation Strategies: FluentValidation in the App Layer Error Handling: Result patterns vs Exceptions
7. Testing Clean Architecture Unit Testing the Domain: Fast and pure Testing Use Cases with Mocks Integration Testing the Infrastructure ArchUnit .NET: Enforcing architectural rules via tests
8. Real-World Case Study Refactoring a 'Spaghetti' Monolith to Clean Architecture DDD in Action: Modeling a complex Logistics system
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details