agentleFS
LeanMeanCleanAppsLeanMeanCleanApps/CleanMinimalApi/.agents/skills/presentation-integration-tests/SKILL.md

presentation-integration-tests

Skill400 starsChanged 10 days agoMITView on GitHub

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 problem details, and testing with authentication on and off. Use when writing or fixing a test in tests/Presentation.Tests.Integration.

4 other projects use a version of this, 4 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Create <Feature>EndpointTests.cs in Endpoints/, IDisposable, with private readonly MinimalApiApplication application.

    line 26
  2. Very specific:

    Write one test per documented status code of the endpoint (test-design).

    line 30

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Presentation 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:

ArgumentUse it to
environmentRun as another environment, such as docker
settingsOverride configuration in memory, such as Authentication:Authority
configureServicesReplace 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

  1. Create <Feature>EndpointTests.cs in Endpoints/, IDisposable, with private readonly MinimalApiApplication application; created in the constructor and disposed in Dispose.
  2. In each test, using var client = this.application.CreateClient();, then send the request with using var response = ....
  3. Assert the status code first, then the body. Read success bodies as the Application entity, failures as ProblemDetails or ValidationProblemDetails, and check Errors or Detail exactly.
  4. 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.
  5. Write one test per documented status code of the endpoint (test-design).
  6. Authentication, in a solution that has it: tests that need it pass Authentication:Authority and Authentication:Audience in settings, and swap the signing key in configureServices. See Authentication/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 JsonSerializerOptions in 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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.