source: update Base rule Agent
This commit is contained in:
@@ -1,32 +1,32 @@
|
||||
---
|
||||
name: GenerateUnitTest
|
||||
description: Hướng dẫn tạo Unit Test cô lập (tốc độ cao) sử dụng Mocking (Moq hoặc NSubstitute).
|
||||
description: Guide to creating isolated Unit Tests (high speed) using Mocking (Moq or NSubstitute).
|
||||
---
|
||||
|
||||
# GenerateUnitTest Skill
|
||||
|
||||
Khi user yêu cầu tạo **Unit Test** cho một class/method, bạn cần tuân thủ các nguyên tắc sau để sinh ra code test:
|
||||
When a user requests a **Unit Test** for a class/method, you must adhere to the following principles to generate the test code:
|
||||
|
||||
## Mục đích
|
||||
- Chỉ test duy nhất 1 class/method.
|
||||
- Bỏ qua hoàn toàn Database thật, Redis hay HTTP.
|
||||
- Tốc độ chạy cực nhanh.
|
||||
## Purpose
|
||||
- Test solely one class/method in isolation.
|
||||
- Completely ignore real Databases, Redis, or HTTP calls.
|
||||
- Extremely fast execution speed.
|
||||
|
||||
## 1. Với CQRS Handlers hoặc Services
|
||||
- **Nhận diện Dependencies:** Tự động nhận diện các Interface (ví dụ: `IRepository`, `IUnitOfWork`, `ILogger`, `IMediator`) được inject vào constructor.
|
||||
- **Tạo Mock Object:** Sử dụng thư viện Mocking (như `Moq` hoặc `NSubstitute`) để tạo instance giả của các Interface này.
|
||||
- **Kịch bản Test (Arrange - Act - Assert):**
|
||||
- **Arrange:** Cấp data giả (Mock data) cho các hàm của Interface.
|
||||
- **Act:** Gọi hàm thực thi (ví dụ `Handle()` của CQRS hoặc các method của Service).
|
||||
- **Assert:** Kiểm tra kết quả trả về, HOẶC verify xem một method của Mock object có được gọi đúng số lần không (ví dụ kiểm tra xem `_repository.AddAsync()` có được gọi không), HOẶC kiểm tra xem nó có ném ra `ValidationException` khi input sai không.
|
||||
## 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. Với Domain Entities
|
||||
- **Mục tiêu:** Kiểm tra các logic kinh doanh, tính toán nội bộ của Entity.
|
||||
- **Kịch bản:**
|
||||
- Khởi tạo Entity với các trạng thái cụ thể.
|
||||
- Gọi method thay đổi trạng thái hoặc tính toán (ví dụ: `Order.CalculateTotal()`).
|
||||
- Kiểm tra xem giá trị biến đổi có đúng với quy tắc nghiệp vụ không (ví dụ: `Total` phải bằng `Price * Quantity`).
|
||||
## 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`).
|
||||
|
||||
## Định dạng file đầu ra
|
||||
- **Đường dẫn:** Đặt trong project `tests/MyNewProjectName.UnitTests/...` (tương ứng với thư mục gốc của class bị test).
|
||||
- **Tên file:** `[ClassName]Tests.cs` (ví dụ: `CreateOrderCommandHandlerTests.cs`).
|
||||
## 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`).
|
||||
|
||||
Reference in New Issue
Block a user