add brain
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
---
|
||||
name: GenerateArchitectureTest
|
||||
description: Guide to creating an Architecture Test using NetArchTest.Rules to protect the Clean Architecture structure.
|
||||
---
|
||||
|
||||
# GenerateArchitectureTest Skill
|
||||
|
||||
When a user requests to verify or create an **Architecture Test**, you must use the `NetArchTest.Rules` library to generate tests that strictly protect the project's "Clean Architecture" boundaries.
|
||||
|
||||
## Purpose
|
||||
- Prevent developers from writing sloppy code by incorrectly importing libraries/modules across layers.
|
||||
- If a violation occurs (e.g., importing Entity Framework into the Domain layer), this test will immediately turn RED during the build process. It acts as solid proof to ensure the architecture is fully protected!
|
||||
|
||||
## Writing Test Rules Guide
|
||||
|
||||
You need to write test methods (using `[Fact]` with xUnit or NUnit) that utilize the Fluent API of `NetArchTest.Rules`. Below are the mandatory common rules:
|
||||
|
||||
1. **Domain Layer Rules:**
|
||||
- The Domain must not depend on anything from Infrastructure, Application, or WebAPI.
|
||||
```csharp
|
||||
[Fact]
|
||||
public void DomainLayer_ShouldNot_HaveDependencyOn_OtherLayers()
|
||||
{
|
||||
var result = Types.InAssembly(DomainAssembly)
|
||||
.ShouldNot()
|
||||
.HaveDependencyOnAny(
|
||||
"MyNewProjectName.Application",
|
||||
"MyNewProjectName.Infrastructure",
|
||||
"MyNewProjectName.WebAPI"
|
||||
)
|
||||
.GetResult();
|
||||
|
||||
Assert.True(result.IsSuccessful);
|
||||
}
|
||||
```
|
||||
|
||||
2. **Application Layer Rules:**
|
||||
- The Application Layer is only allowed to communicate with the Domain. It MUST NOT have dependencies on `Infrastructure` or `WebAPI`.
|
||||
|
||||
3. **Controller Rules (Naming/API Location):**
|
||||
- Controllers must inherit from the BaseAPIController class and have the suffix "Controller".
|
||||
- Direct database queries from the Controller are strictly prohibited (prevent the injection of `DbContext` or `IRepository` into the Controller; verify Constructor dependencies).
|
||||
|
||||
4. **Handler Rules (CQRS):**
|
||||
- Business logic Handlers must implement the `IRequestHandler` interface and end with `CommandHandler` or `QueryHandler`. They must solely reside in the Application Layer.
|
||||
|
||||
## Output File Format
|
||||
- **Path:** Placed within the test project, e.g., `tests/MyNewProjectName.ArchitectureTests/`.
|
||||
- **File Name:** Usually named after the test scope, such as `LayerTests.cs`, `DesignConventionTests.cs`, `NamingRulesTests.cs`, etc.
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: GenerateIntegrationTest
|
||||
description: Guide to creating Integration Tests (end-to-end) using WebApplicationFactory and Testcontainers.
|
||||
---
|
||||
|
||||
# GenerateIntegrationTest Skill
|
||||
|
||||
When a user requests to create an **Integration Test** to verify API endpoints from start to finish, you need to generate the code according to the following guidelines:
|
||||
|
||||
## Purpose
|
||||
- Test whether the assembled components work correctly together.
|
||||
- Flow: Client calls API -> Middleware -> Controller -> CQRS Handler/Service -> Read/Write to a real Database.
|
||||
|
||||
## Implementation Guide
|
||||
1. **Setup WebApplicationFactory:**
|
||||
- Automatically setup `WebApplicationFactory<Program>` (to create an in-memory test server in .NET).
|
||||
- Override application configurations if necessary (e.g., changing the ConnectionString to point to a test container).
|
||||
|
||||
2. **Setup Testcontainers (Real Database):**
|
||||
- Use the `Testcontainers` library (or similar configuration) to automatically spin up a Docker container containing a real Database (e.g., PostgreSQL, SQL Server).
|
||||
- Ensure this database container starts before the tests run and is automatically disposed of after the tests complete. Avoid using in-memory SQLite because it often causes minor bugs and does not accurately mimic a production database.
|
||||
|
||||
3. **Write API Test Scenarios (Arrange - Act - Assert):**
|
||||
- Create an `HttpClient` object from `WebApplicationFactory.CreateClient()`.
|
||||
- **Arrange:** Prepare payload data as JSON objects or prepopulate database records for GET/PUT/DELETE APIs.
|
||||
- **Act:** Call the corresponding API endpoint directly via code. E.g.: `await client.PostAsJsonAsync("/api/v1/samples", payload);`.
|
||||
- **Assert:** Verify the returned results:
|
||||
- Check HTTP Status: is it `200 OK` or `400 Bad Request`?
|
||||
- Deserialize the response body to check the exact object data.
|
||||
- (Optional) Query the database container directly to confirm the record was actually created/updated.
|
||||
|
||||
## Output File Format
|
||||
- **Path:** Placed in the corresponding test project, such as `tests/MyNewProjectName.IntegrationTests/Controllers/...`.
|
||||
- **File Name:** `[ControllerName]Tests.cs` (e.g., `OrdersControllerTests.cs`).
|
||||
32
.brain/.agent/skills/custom-skills/GenerateTest/UnitTest.md
Normal file
32
.brain/.agent/skills/custom-skills/GenerateTest/UnitTest.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
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`).
|
||||
Reference in New Issue
Block a user