Skip to content

Add integration tests using WebApplicationFactory #421

@coderabbitai

Description

@coderabbitai

Problem

The existing unit tests call controller methods directly through Moq mocks and cannot detect routing failures, middleware issues, or HTTP-level serialization problems. There is no test coverage for the actual ASP.NET Core request pipeline.

Proposed Solution

Add an HTTP-layer integration test class using WebApplicationFactory<Program> that exercises the real middleware pipeline, route resolution, status codes, and RFC 7807 Problem Details response bodies — without requiring a running server.

Suggested Approach

  1. Add Microsoft.AspNetCore.Mvc.Testing to the test project.
  2. Expose Program to the test project by adding public partial class Program {} at the bottom of Program.cs.
  3. Create test/.../Integration/PlayerWebApplicationTests.cs using WebApplicationFactory<Program> with an in-memory SQLite database substituted for the real one (via ConfigureTestServices).
  4. Add Utilities/TestAuthHandler.cs to bypass [Authorize] on GET /players/{id:Guid}.
  5. Use [Trait("Category", "Integration")] on all tests in this class.
  6. Follow the controller naming convention: {HttpMethod}_{Resource}_{Condition}_Returns{Outcome}.

Endpoints to cover

GET /players

  • Returns 200 OK with all seeded players

GET /players/{id:Guid}

  • Returns 200 OK with a valid player body when the ID exists
  • Returns 404 Not Found (RFC 7807) when not found

GET /players/squadNumber/{squadNumber:int}

  • Returns 200 OK with a valid player body when the squad number exists
  • Returns 404 Not Found (RFC 7807) when not found

POST /players

  • Returns 201 Created with valid payload
  • Returns 400 Bad Request (RFC 7807) when validation fails
  • Note: the 409 Conflict branch in the controller is unreachable via the HTTP pipeline. The BeUniqueSquadNumber rule in the "Create" validation rule set catches duplicate squad numbers first and returns 400. The 409 path is covered by the unit test Post_Players_Existing_Returns409Conflict, where validation is mocked to pass.

PUT /players/squadNumber/{squadNumber:int}

  • Returns 204 No Content on successful update
  • Returns 404 Not Found (RFC 7807) when player does not exist
  • Returns 400 Bad Request (RFC 7807) when validation fails
  • Returns 400 Bad Request (RFC 7807) when squad number in route does not match body

DELETE /players/squadNumber/{squadNumber:int}

  • Returns 204 No Content on successful deletion
  • Returns 404 Not Found (RFC 7807) when player does not exist

GET /health

  • Returns 200 OK

Acceptance Criteria

  • Microsoft.AspNetCore.Mvc.Testing added to the test project
  • public partial class Program {} added to Program.cs
  • Utilities/TestAuthHandler.cs added to bypass [Authorize] in tests
  • test/.../Integration/PlayerWebApplicationTests.cs exists
  • All endpoints listed above have at least the happy-path and principal error cases covered
  • All error response assertions validate the RFC 7807 schema (type, title, status)
  • [Trait("Category", "Integration")] applied to all tests in the class
  • All existing tests continue to pass
  • CHANGELOG.md updated

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codeenhancementNew feature or requestplanningEnables automatic issue planning with CodeRabbit

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions