source: update Base rule Agent
This commit is contained in:
@@ -1,34 +1,34 @@
|
||||
---
|
||||
name: GenerateIntegrationTest
|
||||
description: Hướng dẫn tạo Integration Test (người thật việc thật) sử dụng WebApplicationFactory và Testcontainers.
|
||||
description: Guide to creating Integration Tests (end-to-end) using WebApplicationFactory and Testcontainers.
|
||||
---
|
||||
|
||||
# GenerateIntegrationTest Skill
|
||||
|
||||
Khi user yêu cầu tạo **Integration Test** để kiểm tra API endpoint từ đầu đến cuối, bạn cần sinh ra code theo hướng dẫn sau:
|
||||
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:
|
||||
|
||||
## Mục đích
|
||||
- Test xem các mảnh ghép lắp vào ghép với nhau có chạy đúng không.
|
||||
- Flow: Client gọi API -> Middleware -> Controller -> CQRS Handler/Service -> Ghi/Đọc Database thật.
|
||||
## Purpose
|
||||
- Test whether the assembled components work correctly together.
|
||||
- Flow: Client calls API -> Middleware -> Controller -> CQRS Handler/Service -> Read/Write to a real Database.
|
||||
|
||||
## Hướng dẫn thực hiện
|
||||
## Implementation Guide
|
||||
1. **Setup WebApplicationFactory:**
|
||||
- Tự động setup `WebApplicationFactory<Program>` (tạo một test server ngay trong RAM của .NET).
|
||||
- Override cấu hình ứng dụng nếu cần thiết (ví dụ thay đổi ConnectionString trỏ sang test container).
|
||||
- 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):**
|
||||
- Sử dụng thư viện `Testcontainers` (hoặc cấu hình tương tự) để tự động spin up một Docker container chứa Database thật (ví dụ: PostgreSQL, SQL Server).
|
||||
- Đảm bảo database container này được start trước khi chạy test và tự động xóa (dispose) sau khi test xong. Tránh dùng SQLite in-memory vì hay bị lỗi vặt và không tương đương với database production.
|
||||
- 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. **Viết kịch bản Test gọi API (Arrange - Act - Assert):**
|
||||
- Tạo đối tượng `HttpClient` từ `WebApplicationFactory.CreateClient()`.
|
||||
- **Arrange:** Chuẩn bị payload data dạng JSON objects hoặc tạo trước data base records nếu là API GET/PUT/DELETE.
|
||||
- **Act:** Gọi thẳng vào API endpoint tương ứng bằng code. VD: `await client.PostAsJsonAsync("/api/v1/samples", payload);`.
|
||||
- **Assert:** Kiểm tra kết quả trả về:
|
||||
- Check HTTP Status: có phải `200 OK` hay `400 Bad Request` không.
|
||||
- Deserialize response body để check chính xác object.
|
||||
- (Tùy chọn) Query thẳng vào database container xem bản ghi đã được sinh ra/cập nhật thật chưa.
|
||||
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.
|
||||
|
||||
## Định dạng file đầu ra
|
||||
- **Đường dẫn:** Chứa trong project test tương ứng như `tests/MyNewProjectName.IntegrationTests/Controllers/...`.
|
||||
- **Tên file:** `[ControllerName]Tests.cs` (ví dụ: `OrdersControllerTests.cs`).
|
||||
## 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`).
|
||||
|
||||
Reference in New Issue
Block a user