Skip to content

refactor(tests): more idiomatic Fake methods and result assertions #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025

Conversation

nanotaboada
Copy link
Owner

@nanotaboada nanotaboada commented Apr 12, 2025

Summary by CodeRabbit

  • Refactor
    • Standardized naming conventions for player data operations to improve code consistency and maintainability.
  • Tests
    • Updated unit tests to align with the refined naming, ensuring clearer request/response handling and more reliable validations.

These changes do not affect functionality for end-users but enhance the application's internal quality and test reliability.

Copy link

coderabbitai bot commented Apr 12, 2025

Walkthrough

This pull request focuses on renaming methods for consistency in generating and retrieving player data. The method previously named CreateStarting11 has been renamed to MakeStarting11 in the main data class, and corresponding calls throughout the application and test code have been updated. In addition, test utilities have been refactored to use the new naming convention, with variable names and method calls adjusted to reflect a standardized "Make" prefix. No changes have been made to the underlying logic or control flow.

Changes

Files Change Summary
src/.../Data/PlayerData.cs Renamed method CreateStarting11 to MakeStarting11 in the PlayerData class.
src/.../Utilities/ApplicationBuilderExtensions.cs, src/.../Utilities/DbContextUtils.cs Updated method calls in seeding routines, replacing PlayerData.CreateStarting11() with PlayerData.MakeStarting11().
test/.../Unit/PlayerControllerTests.cs, test/.../Unit/PlayerServiceTests.cs Updated tests by renaming variables (e.g., payload to request, response to httpResult) and replacing calls to old model creation methods with new ones (e.g., MakeResponseModelForCreate, MakeResponseModelForRetrieve).
test/.../Utilities/DatabaseFakes.cs, test/.../Utilities/PlayerFakes.cs Renamed fake data methods: changed GetStarting11 to MakeStarting11; updated several factory method names from Create* to Make*; removed CreateStarting11ResponseModels and added new methods (MakeFromStarting11ById, MakeResponseModelsForRetrieve, etc.).

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.00% 0.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (d9101db) 799 186 23.28%
Head commit (d1404a2) 799 (+0) 186 (+0) 23.28% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#206) 2 0 0.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Copy link

codecov bot commented Apr 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (d9101db) to head (d1404a2).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #206   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          173       173           
  Branches        14        14           
=========================================
  Hits           173       173           
Components Coverage Δ
Controllers 100.00% <ø> (ø)
Services 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (1)

284-299: Added method to create response models list

The new MakeResponseModelsForRetrieve() method efficiently creates a list of response models from the starting eleven players, reducing code duplication in test methods.

There's some code duplication between this method and other response model creation methods, such as MakeResponseModelForCreate() and MakeResponseModelForRetrieve(). Consider extracting the player-to-response-model mapping logic into a separate private method to reduce duplication:

+ private static PlayerResponseModel MapPlayerToResponseModel(Player player)
+ {
+     return new PlayerResponseModel
+     {
+         Id = player.Id,
+         FullName =
+             $"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
+         Birth = $"{player.DateOfBirth:MMMM d, yyyy}",
+         Dorsal = player.SquadNumber,
+         Position = player.Position,
+         Club = player.Team,
+         League = player.League,
+         Starting11 = player.Starting11 ? "Yes" : "No"
+     };
+ }

Then update the response model methods to use this shared mapping method.

🧰 Tools
🪛 GitHub Check: Codeac Code Quality

[warning] 287-299: CodeDuplication
This block of 12 lines is too similar to test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs:226

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d9101db and d1404a2.

📒 Files selected for processing (7)
  • src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs (1 hunks)
  • src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (1 hunks)
  • src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1 hunks)
  • test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs (30 hunks)
  • test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs (14 hunks)
  • test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs (1 hunks)
  • test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (7 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (5)
src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs (2)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (4)
  • List (15-171)
  • List (284-299)
  • Player (173-180)
  • Player (182-198)
src/Dotnet.Samples.AspNetCore.WebApi/Models/Player.cs (1)
  • Player (11-34)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs (1)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (1)
  • PlayerFakes (13-314)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs (1)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (7)
  • PlayerFakes (13-314)
  • PlayerResponseModel (222-238)
  • PlayerResponseModel (264-282)
  • PlayerResponseModel (310-313)
  • PlayerRequestModel (204-220)
  • PlayerRequestModel (244-262)
  • PlayerRequestModel (305-308)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs (1)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (8)
  • PlayerFakes (13-314)
  • PlayerResponseModel (222-238)
  • PlayerResponseModel (264-282)
  • PlayerResponseModel (310-313)
  • List (15-171)
  • List (284-299)
  • Player (173-180)
  • Player (182-198)
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (4)
src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs (2)
  • List (9-165)
  • List (173-331)
src/Dotnet.Samples.AspNetCore.WebApi/Models/Player.cs (1)
  • Player (11-34)
src/Dotnet.Samples.AspNetCore.WebApi/Models/PlayerRequestModel.cs (1)
  • PlayerRequestModel (13-32)
src/Dotnet.Samples.AspNetCore.WebApi/Models/PlayerResponseModel.cs (1)
  • PlayerResponseModel (11-28)
🪛 GitHub Check: Codeac Code Quality
test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs

[warning] 287-299: CodeDuplication
This block of 12 lines is too similar to test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs:226

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Codacy Security Scan
🔇 Additional comments (27)
src/Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs (1)

9-9: Consistent method naming looks good.

The renaming from CreateStarting11 to MakeStarting11 is a good refactoring that establishes a more consistent naming convention for factory methods across the codebase.

src/Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (1)

24-24: Updated method reference correctly.

The reference to PlayerData.MakeStarting11() has been correctly updated to match the renamed method in the PlayerData class.

src/Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1)

38-38: Method reference updated properly.

The call to PlayerData.MakeStarting11() has been updated correctly to align with the renamed method.

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/DatabaseFakes.cs (1)

51-51: Method reference updated to use consistent naming.

The change from PlayerFakes.GetStarting11() to PlayerFakes.MakeStarting11() correctly aligns with the standardized naming convention using the "Make" prefix instead of "Get" for factory methods.

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs (9)

30-30: Variable renaming follows more idiomatic convention

The variable name request is more descriptive of its purpose than the previous name, making it clear that this is a request object being passed to the controller.


59-63: Improved result assertion variable naming

Changing from response to httpResult makes the code clearer by distinguishing between the HTTP result object and the response model content. This makes assertions more readable and aligns with ASP.NET Core minimal API patterns.


71-73: Consistent naming of mock setup variables

The renaming from payload to request and updated response model creation method names provide better consistency throughout the test class, making the intent clearer.


114-117: More explicit test data setup

Using explicit ID value and the new fake methods improves test readability by clearly showing the relationship between test data objects.


150-157: Improved type validation in assertions

The assertion is now correctly checking for Created<PlayerResponseModel> instead of using a generic PlayerRequestModel, which more accurately reflects the expected return type from the controller method.


169-171: Using new response model creation method

The change to MakeResponseModelsForRetrieve() helps maintain consistency with the naming convention changes while ensuring the test behavior remains the same.


325-327: More explicit request setup for validation test

Using the new fake method and explicit invalidation of the squad number improves test readability by clearly showing what's being tested.


411-415: More comprehensive request model setup

Using the fake methods and explicit property assignments provides better clarity about what's being modified in the update test scenario.


435-439: Consistent HTTP result assertion pattern

The renaming to httpResult continues the pattern established throughout the class, improving readability and maintainability.

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerServiceTests.cs (7)

28-31: Using more idiomatic fake methods

Changed variable setup to use the renamed methods MakeRequestModelForCreate() and MakeResponseModelForCreate(), which improves code readability and aligns with the standardized "Make" prefix convention.


61-66: Updated variable definitions with standardized method names

The service test now correctly uses the renamed methods MakeStarting11() and MakeResponseModelsForRetrieve(), maintaining test functionality while improving naming consistency.


131-133: Using explicit ID values instead of generic matchers

Using a specific ID value (999) makes the test more concrete and easier to understand compared to using It.IsAny<long>(), which aligns with the goal of writing more idiomatic tests.


156-161: More specific test data initialization

Using explicit ID reference and the new MakeFromStarting11ById(id) method creates a clearer relationship between the test data objects, improving test readability.


215-218: Better relationship between player ID and squad number

By first retrieving a specific player by ID and then using that player's squad number, the test more accurately reflects real-world usage patterns and maintains data consistency.


254-257: Using more concrete and descriptive test data

The test now uses specific player ID and request model values rather than generic ones, making the test more readable and maintainable.


289-294: More explicit test arrangement

Using a specific player ID and retrieving a player with the new helper method makes the test setup more clear and aligns with the new naming convention.

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs (7)

15-15: Renamed method follows consistent naming convention

Changed from GetStarting11() to MakeStarting11() to align with the standardized "Make" prefix convention used throughout the class.


173-180: Added utility method to retrieve players by ID

This new method MakeFromStarting11ById(long id) reduces code duplication by providing a standard way to retrieve a player by ID from the starting eleven list.


182-182: Renamed method for creating a new player instance

Changed from CreateOneNew() to MakeNew() to align with the standardized "Make" prefix convention.


200-203: Added section comments for better code organization

The addition of section comments helps organize the code by operation type (Create, Retrieve, Update), making it easier to navigate and understand.


204-220: Renamed request model creation method

Changed from CreateRequestModelForOneNew() to MakeRequestModelForCreate() to align with the standardized "Make" prefix convention while maintaining the same functionality.


222-238: Renamed response model creation method

Changed from CreateResponseModelForOneNew() to MakeResponseModelForCreate() to align with the standardized "Make" prefix convention while maintaining the same functionality.

🧰 Tools
🪛 GitHub Check: Codeac Code Quality

[warning] 226-238: CodeDuplication
This block of 12 lines is too similar to test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs:287


305-313: Added update model creation methods

The new methods MakeRequestModelForUpdate(long id) and MakeResponseModelForUpdate(long id) provide a clean way to reuse the retrieve methods for update operations, maintaining consistency and reducing code duplication.

@nanotaboada nanotaboada merged commit 82a9fcf into master Apr 12, 2025
23 of 24 checks passed
@nanotaboada nanotaboada deleted the feature/idiomatic-fakes-and-assertions branch April 12, 2025 03:42
@coderabbitai coderabbitai bot mentioned this pull request Apr 15, 2025
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.

1 participant