How do you test an ASP.NET Core endpoint across validation, authorization, database, and external-service boundaries?
Razorpay · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, ASP.NET Core, Testing
Community answers
Posted by wasAsked editorial collection
Test an endpoint through the real ASP.NET Core pipeline with WebApplicationFactory so routing, model binding, filters, authentication, serialization, and persistence participate.
Example:
var client = factory.CreateClient();
client.DefaultRequestHeaders.Authorization = TestToken.For(user);
var response = await client.PostAsJsonAsync("/api/orders", request);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Use an isolated real database compatible with production, reset it between tests, and seed only through supported boundaries where practical. Cover success, invalid input, unauthenticated/forbidden access, duplicate/idempotent calls, concurrency, dependency failure, and RestaurantId/OutletId isolation. Assert response contracts and database state, not private implementation calls.
Backbencher Developer · · 0 likes
Test each boundary at the level where it is cheapest to test properly.
Validation
- Unit-test validators (FluentValidation or data annotation logic) with many input cases.
- In integration tests, send a few invalid requests and assert 400 with the expected Problem Details field errors.
Authorization
- Use WebApplicationFactory and replace authentication with a test scheme that creates users with specific roles, claims and tenant IDs:
builder.ConfigureTestServices(s => s.AddAuthentication("Test")
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", _ => { }));
- Test anonymous (401), wrong role (403), other tenant's resource (404 or 403) and allowed user (2xx). These tests catch the most dangerous bugs.
Database
- Run against the same engine as production in a disposable container (Testcontainers for SQL Server or PostgreSQL), applying real migrations.
- Reset data between tests with Respawn or transactions, and seed with builders.
- Assert on database state after write endpoints, not only on the response.
- Avoid the EF in-memory provider for these tests: it ignores constraints, transactions and SQL translation.
External services
- Replace HTTP clients with stubs: WireMock.Net for realistic HTTP behaviour (timeouts, 500s, slow responses), or a fake implementation registered in ConfigureTestServices.
- Test failure paths: provider down, timeout, retries, circuit breaker, and idempotency of retried writes.
- Keep a small number of separately scheduled tests against the provider's sandbox to detect contract changes.
Structure
- One test class per endpoint or feature with clear Arrange-Act-Assert.
- Typed HttpClient helpers for common calls; use the real JSON serializer settings.
- Run in CI on every pull request; parallelise where database isolation allows.
Example assertion set for POST /payments: invalid amount gives 400, missing token gives 401, another merchant gives 403, provider timeout gives 503 with no duplicate charge, success gives 201 with a database row and outbox message.