--- name: GenerateUnitTest description: Guide to creating isolated Unit Tests (high speed) using Mocking (Moq or NSubstitute). --- # GenerateUnitTest Skill When a user requests a **Unit Test** for a class/method, you must adhere to the following principles to generate the test code: ## Purpose - Test solely one class/method in isolation. - Completely ignore real Databases, Redis, or HTTP calls. - Extremely fast execution speed. ## 1. For CQRS Handlers or Services - **Identify Dependencies:** Automatically identify the Interfaces (e.g., `IRepository`, `IUnitOfWork`, `ILogger`, `IMediator`) injected into the constructor. - **Create Mock Objects:** Use a Mocking library (like `Moq` or `NSubstitute`) to create fake instances of these Interfaces. - **Test Scenario (Arrange - Act - Assert):** - **Arrange:** Provide fake data (Mock data) for the Interface methods. - **Act:** Call the method being executed (e.g., `Handle()` in CQRS or Service methods). - **Assert:** Check the returned result, OR verify if a Mock object's method was called the correct number of times (e.g., checking if `_repository.AddAsync()` was called), OR verify if it throws a `ValidationException` given invalid input. ## 2. For Domain Entities - **Goal:** Verify internal business logic and calculations of the Entity. - **Scenario:** - Initialize the Entity with specific states. - Call a method that alters the state or performs a calculation (e.g., `Order.CalculateTotal()`). - Verify if the modified value adheres to business rules (e.g., `Total` must equal `Price * Quantity`). ## Output File Format - **Path:** Placed in the `tests/MyNewProjectName.UnitTests/...` project (corresponding to the tested class's root directory). - **File Name:** `[ClassName]Tests.cs` (e.g., `CreateOrderCommandHandlerTests.cs`).