--- 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` (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`).