Skip to content

Add integration tests for Repository<T> and PlayerRepository #461

@nanotaboada

Description

@nanotaboada

Problem

The three existing test files mock everything at or above the service layer. Repository<T> and PlayerRepository have zero test coverage — the service tests verify that repository methods are called, never that they work. Several non-trivial behaviors are untested:

Method Untested behavior
GetAllAsync() AsNoTracking() — correctness concern for read performance
FindByIdAsync() Returns null for a missing entity
RemoveAsync() Null guard (if (entity != null)) — neither branch is exercised
FindBySquadNumberAsync() Real FirstOrDefaultAsync query against the DB
SquadNumberExistsAsync() Real AnyAsync query — on the critical path for the 409 Conflict response on POST, never tested against an actual database

Proposed Solution

Add a PlayerRepositoryTests class covering the non-trivial behaviors using DatabaseFakes.MigrateAsync() on in-memory SQLite (already available after #459). Scope tightly — test behaviors that have real logic, not boilerplate EF Core delegation.

Roughly 8–10 tests:

  • GetAllAsync — returns all seeded players
  • FindByIdAsync — found (existing player) and not found (unknown ID)
  • RemoveAsync — entity found (removes it) and entity not found (no-op, no exception)
  • FindBySquadNumberAsync — found and not found
  • SquadNumberExistsAsync — exists and does not exist

Suggested Approach

  1. Create test/.../Integration/ subfolder to distinguish from pure unit tests.
  2. Add PlayerRepositoryTests.cs using DatabaseFakes.MigrateAsync() for setup — this also validates the full migration chain as a side effect.
  3. Use [Trait("Category", "Integration")] on all tests in this class.
  4. Follow the service/validator naming convention: {MethodName}_{StateUnderTest}_{ExpectedBehavior}.
  5. Use PlayerData.MakeStarting11WithId() or PlayerFakes for known entities; use Guid.NewGuid() for unknown IDs.

Acceptance Criteria

  • test/.../Integration/PlayerRepositoryTests.cs exists
  • All methods in Repository<T> and PlayerRepository with non-trivial logic are covered
  • Both branches of RemoveAsync are tested
  • Both branches of SquadNumberExistsAsync are tested
  • Tests use DatabaseFakes.MigrateAsync() — no manual schema creation or seeding
  • [Trait("Category", "Integration")] applied to all tests in the class
  • All existing tests continue to pass
  • CHANGELOG.md updated

Notes

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codeenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions