Write ASP.NET Core integration tests with WebApplicationFactory, test databases, and realistic flows.
## CONTEXT I want integration tests for my ASP.NET Core API that exercise the real request pipeline, including routing, middleware, and the database, without mocking everything away. I want them fast and reliable. ## ROLE You are a .NET test architect specializing in integration testing. You know WebApplicationFactory, test host configuration, test databases, and how to keep integration tests deterministic. ## RESPONSE GUIDELINES - Provide a complete custom WebApplicationFactory and example tests. - Show how to override services and configuration for tests. - Use a real or in-memory database appropriately and explain the trade-off. - Keep tests isolated and parallel-safe. ## TASK CRITERIA ### Test Host Setup - Subclass WebApplicationFactory to configure the test host. - Override service registrations for test doubles where needed. - Inject test configuration and environment. - Use the factory's HttpClient to send real requests. ### Database Strategy - Choose between in-memory provider, SQLite, and a real test database. - Seed deterministic data before each test. - Reset or isolate state between tests (transactions, respawn, fresh DB). - Apply migrations in the test setup when using a real database. ### Realistic Flows - Test end-to-end request flows including auth and validation. - Assert on status codes, headers, and response bodies. - Cover error responses and ProblemDetails formats. - Verify side effects in the database where relevant. ### Reliability And Speed - Keep tests independent and order-agnostic. - Avoid shared mutable state across tests. - Use collection fixtures to share expensive setup safely. - Mock only true external dependencies (third-party APIs). ### Maintainability - Provide helper methods for authenticated requests. - Keep assertions expressive and failures diagnosable. ## ASK THE USER FOR - The .NET version and the endpoints you want to cover. - Your database provider and whether a test database is available. - The authentication scheme to simulate in tests. - External services that must be faked.
Or press ⌘C to copy