Skip to content

Conversation

Copy link

Copilot AI commented Oct 1, 2025

Overview

This PR adds comprehensive unit test coverage for the User Profile REST API application using xUnit, Moq, and FluentAssertions. All 20 tests pass successfully, covering existing controller actions and model functionality.

Changes Made

Test Project Setup

  • Created net-users-api.tests project at solution level following project structure guidelines
  • Added xUnit 2.9.2 as the testing framework
  • Added Moq 4.20.72 for mocking ILogger dependencies
  • Added FluentAssertions 8.7.0 for readable, expressive assertions
  • Configured project reference to main API project
  • Added test project to solution file

Test Coverage (20 Tests - 100% Passing)

HomeController Tests (2 tests)

Tests for the MVC controller that renders the HTML table view:

  • Verifies Index action returns ViewResult with user list model
  • Verifies logging behavior when endpoint is called

UsersController Tests (15 tests)

Comprehensive coverage of all REST API endpoints:

GetUsers (2 tests)

  • Returns OK result with list of users
  • Logs information message on invocation

GetUser (3 tests)

  • Returns OK with user when valid ID provided
  • Returns NotFound when invalid ID provided
  • Returns NotFound with error message for non-existent users

CreateUser (4 tests)

  • Returns CreatedAtAction with proper route values for valid user
  • Successfully adds user to the internal list
  • Returns BadRequest when null user provided
  • Returns BadRequest with error message for invalid data

UpdateUser (6 tests)

  • Returns OK with updated user for valid ID and data
  • Ensures ID from route parameter is preserved (not overwritten by body)
  • Returns NotFound when updating non-existent user
  • Returns NotFound with error message for non-existent users
  • Returns BadRequest when null user provided
  • Returns BadRequest with error message for invalid data

UserProfile Model Tests (3 tests)

  • Verifies model can be created with all required properties
  • Confirms properties can be modified after creation
  • Tests ID property setter behavior

Testing Approach

All tests follow .NET best practices:

  • Arrange-Act-Assert pattern used consistently
  • Dependency injection properly mocked using Moq
  • Both success and failure paths tested for each endpoint
  • Edge cases covered (null inputs, non-existent IDs, validation)
  • ILogger dependencies isolated to focus on controller logic

Important Note

Per the issue requirements, only existing implemented code is tested. The DeleteUser endpoint is not implemented (throws NotImplementedException) and therefore has no tests. This aligns with the requirement to not implement missing functionality.

Running Tests

# Run all tests
dotnet test

# Run with detailed output
dotnet test --logger "console;verbosity=detailed"

# Run without rebuild
dotnet test --no-build

Test Results

Test Run Successful.
Total tests: 20
     Passed: 20
     Failed: 0
  Skipped: 0

Files Added

net-users-api.tests/
├── Controllers/
│   ├── HomeControllerTests.cs       (2 tests)
│   └── UsersControllerTests.cs      (15 tests)
├── Models/
│   └── UserProfileTests.cs          (3 tests)
└── net-users-api.tests.csproj

Closes #[issue-number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Implement Unit Tests for User Profile REST API</issue_title>
<issue_description>Add comprehensive unit tests for the User Profile REST API application. The tests should cover all controller functions, including:

• HomeController (Index action)
• UsersController.GetUsers
• UsersController.GetUser
• UsersController.CreateUser
• UsersController.UpdateUser
• UsersController.DeleteUser (Note: This function is currently not implemented - tests should only be written for existing, implemented functions)

IMPORTANT REQUIREMENT: Only implement test cases for existing code. Do not implement any missing functionality or complete unfinished code. Focus solely on testing the current implemented functions.

Acceptance Criteria:

• Each controller action has corresponding unit tests.
• Tests cover both successful and failure scenarios (e.g., user not found, invalid input).
• Use xUnit testing framework with Moq for mocking dependencies.
• Ensure tests are easy to run (e.g., via dotnet test).
• All tests must pass.
• Do not implement the DeleteUser function or any other missing functionality - only write tests for existing code.

Additional Notes:

• Follow .NET testing best practices.
• Mock or isolate dependencies (ILogger, etc.) as needed.
• Place test project at solution level as net-users-api.tests/ following the project structure guidelines.
• Create test classes like UsersControllerTests.cs and UserProfileTests.cs in appropriate folders mirroring the main project structure (Controllers/, Models/).
• If a function is not implemented (like DeleteUser), skip writing tests for it until the function is properly implemented in a separate issue.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #1

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Implement Unit Tests for User Profile REST API Implement comprehensive unit tests for User Profile REST API Oct 1, 2025
Copilot AI requested a review from frye October 1, 2025 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Unit Tests for User Profile REST API

2 participants