presentation-integration-tests
How this Minimal API is tested end to end with WebApplicationFactory - the MinimalApiApplication host, a private database per test class, overriding settings and services, the JSON helper, asserting p
Files1
View on GitHubSKILL.md.agents/skills/presentation-integration-tests/SKILL.md
GitHubPresentation integration tests (Minimal API)
The rule
tests/Presentation.Tests.Integration makes real HTTP requests to the whole application in memory. MinimalApiApplication (a WebApplicationFactory<Program>) starts the app with the local environment, its own shared-cache in-memory SQLite database and the sample data on, so tests never touch a file or each other.
Each test class creates one MinimalApiApplication in its constructor and disposes it. The constructor takes three optional arguments:
| Argument | Use it to |
|---|---|
environment |
Run as another environment, such as docker |
settings |
Override configuration in memory, such as Authentication:Authority |
configureServices |
Replace or post-configure services, such as the JWT signing key |
Responses are read with the Deserialize<T>() helper in Extensions/JsonSerializerExtensions.cs, which uses the API's own JSON options (json-serialization).
Steps
- Create
<Feature>EndpointTests.csinEndpoints/,IDisposable, withprivate readonly MinimalApiApplication application;created in the constructor and disposed inDispose. - In each test,
using var client = this.application.CreateClient();, then send the request withusing var response = .... - Assert the status code first, then the body. Read success bodies as the Application entity, failures as
ProblemDetailsorValidationProblemDetails, and checkErrorsorDetailexactly. - Find the rows you need through the API (list, then pick one with the property you need), not by hard-coded ids. Sample data ids are random.
- Write one test per documented status code of the endpoint (
test-design). - Authentication, in a solution that has it: tests that need it pass
Authentication:AuthorityandAuthentication:Audienceinsettings, and swap the signing key inconfigureServices. SeeAuthentication/AuthenticationTests.cs.
Example
// tests/Presentation.Tests.Integration/Endpoints/ReviewEndpointTests.cs
[Fact]
public async Task CreateReview_ShouldReturn_ValidationProblem()
{
// Arrange
using var client = this.application.CreateClient();
var json = (new { Stars = 0, AuthorId = Guid.Empty, MovieId = Guid.Empty }).Serialize();
using var content = new StringContent(json, Encoding.UTF8, "application/json");
// Act
using var response = await client.PostAsync("/api/reviews", content);
var result = (await response.Content.ReadAsStringAsync()).Deserialize<ValidationProblemDetails>();
// Assert
response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
_ = result.ShouldNotBeNull();
result.Errors["Stars"].ShouldBe(["A star rating must be between 1 and 5."]);
}
// tests/Presentation.Tests.Integration/Authentication/AuthenticationTests.cs (only with --include-auth)
private readonly MinimalApiApplication application = new(
settings: new Dictionary<string, string?>
{
["Authentication:Authority"] = Authority,
["Authentication:Audience"] = Audience
},
configureServices: services => services.PostConfigure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.Configuration = new OpenIdConnectConfiguration { Issuer = Authority };
options.TokenValidationParameters.IssuerSigningKey = SigningKey;
options.TokenValidationParameters.ValidIssuer = Authority;
}));
Don't
- Share one application across test classes with
IClassFixture. Each class gets its own database. - Edit appsettings files for a test. Use
settings. - Build your own
JsonSerializerOptionsin a test. - Assert only the status code when the endpoint returns a body.
- Depend on the order rows come back in unless the endpoint promises one.
Check
dotnet test tests/Presentation.Tests.Integration