Skip to content

Conversation

@T-rav
Copy link
Contributor

@T-rav T-rav commented May 28, 2025

This pull request introduces a comprehensive suite of unit tests for the MockLogger, MockOptions, and MockOptionsSnapshot classes in the StoneAge.TestUtils library. The following changes have been made:

  1. MockLoggerTests.cs: Added tests to verify logging functionality, including checking if log entries are added correctly, if the logger is always enabled, and if the logger fetches entries as expected.
  2. MockOptionsSnapshotTests.cs: Added tests to ensure that the MockOptionsSnapshot returns the injected instance correctly and throws a NotImplementedException for the Get method.
  3. MockOptionsTests.cs: Added tests to confirm that the MockOptions class returns the correct injected instance.
  4. TestHttpClientBuilderTests.cs: Added tests to validate the behavior of the TestHttpClientBuilder when creating clients with specified and default payloads.
  5. TestHttpMessageHandlerTests.cs: Added tests to ensure that the TestHttpMessageHandler returns specified payloads in order and sets the request message correctly.
  6. StoneAge.TestUtils.Tests.csproj: Created a new test project with necessary dependencies for xUnit testing.
  7. TestUtils.sln: Updated the solution file to include the new test project.

These changes enhance the test coverage of the StoneAge.TestUtils library, ensuring better reliability and maintainability.


This pull request was co-created with Cosine Genie

Original Task: DotNetCoreTestUtils/1xf6kjvb55l5
Author: Travis Frisinger

Summary by CodeRabbit

  • New Features

    • Introduced a comprehensive test suite for the project, including tests for logging, options snapshot, options, HTTP client builder, and HTTP message handler functionalities.
    • Added a new test project to the solution, enabling automated testing and validation of core behaviors.
  • Chores

    • Updated the solution to include the new test project and adjusted configuration for compatibility.

…ClientBuilder

Co-authored-by: Genie <genie@cosine.sh>
@coderabbitai
Copy link

coderabbitai bot commented May 28, 2025

Walkthrough

A new test project for the StoneAge.TestUtils library has been established, including multiple test classes to validate core utility components such as logging, options snapshots, and HTTP client behaviors. The solution and project files were updated to incorporate this test project and ensure compatibility with .NET 6.0 and xUnit.

Changes

File(s) Change Summary
StoneAge.TestUtils.Tests/MockLoggerTests.cs Added tests for MockLogger: verifies logging, log level enablement, entry fetching, and scope exception.
StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs Added tests for MockOptionsSnapshot: checks value retrieval and exception on Get method.
StoneAge.TestUtils.Tests/MockOptionsTests.cs Added tests for MockOptions: ensures injected instance is returned by Value property.
StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs Added tests for TestHttpClientBuilder: validates payload handling and default payload behavior.
StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs Added tests for TestHttpMessageHandler: checks payload sequencing, default payload, and request capture.
StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj New .NET 6.0 test project file; references xUnit, test SDK, and main project.
TestUtils.sln Solution updated: added test project, updated GUIDs, and project configuration for new test project.

Poem

In the warren of code, new tests now hop,
Checking logs, options, and HTTP atop.
Snapshots and handlers, all put to the test,
Ensuring our utils are truly the best.
With xUnit in tow and .NET in the sun,
This rabbit declares: the testing’s begun!
🐇✨

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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

@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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 11d3a7e and 283451d.

📒 Files selected for processing (7)
  • StoneAge.TestUtils.Tests/MockLoggerTests.cs (1 hunks)
  • StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs (1 hunks)
  • StoneAge.TestUtils.Tests/MockOptionsTests.cs (1 hunks)
  • StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj (1 hunks)
  • StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs (1 hunks)
  • StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs (1 hunks)
  • TestUtils.sln (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
StoneAge.TestUtils.Tests/MockLoggerTests.cs (1)
TestUtils/MockLogger.cs (2)
  • Log (17-29)
  • IsEnabled (36-39)
🔇 Additional comments (13)
TestUtils.sln (1)

6-8: LGTM! Test project integration looks correct.

The new test project has been properly added to the solution with appropriate GUID and project reference structure.

StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj (1)

1-18: LGTM! Standard test project configuration.

The project file follows .NET testing conventions correctly with appropriate target framework, test dependencies, and project references.

StoneAge.TestUtils.Tests/MockOptionsTests.cs (1)

1-23: LGTM! Well-structured unit test.

The test follows excellent practices:

  • Clear, descriptive test method name
  • Simple test data setup with nested Settings class
  • Comprehensive assertions testing both property value and reference equality
  • Proper use of xUnit assertions
StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs (1)

1-33: LGTM! Comprehensive test coverage.

Excellent test implementation that covers both positive and negative scenarios:

  • Tests the Value property returns the correct injected instance
  • Verifies the Get method throws NotImplementedException as expected
  • Uses descriptive test method names and clear assertions
  • Proper use of xUnit testing patterns
StoneAge.TestUtils.Tests/MockLoggerTests.cs (4)

11-18: LGTM! Well-structured test for basic logging functionality.

The test correctly verifies that log entries are added to the appropriate log level collection. The use of a lambda formatter (s, e) => s is appropriate for testing purposes.


20-26: LGTM! Good coverage of the IsEnabled behavior.

The test correctly verifies that the MockLogger always returns true for IsEnabled, testing multiple log levels to ensure consistency with the implementation shown in the relevant code snippets.


28-37: LGTM! Comprehensive test for entry retrieval functionality.

The test properly verifies that the Fetch_Entries_For method returns the correct entries for a specific log level. Using Assert.Single and Assert.Equal with entries.First() provides good validation of both the count and content.


39-44: LGTM! Proper testing of unimplemented functionality.

The test correctly verifies that BeginScope throws NotImplementedException, which is appropriate for a mock implementation that doesn't need to support scoped logging.

StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs (2)

10-22: LGTM! Comprehensive test for custom payload functionality.

The test effectively verifies that the builder creates a client that returns the specified payload and properly captures request information. The URI assertion on line 21 shows the expected behavior where HttpClient normalizes "http://test" to "http://test/" by adding a trailing slash.


24-34: LGTM! Good test for default behavior.

The test correctly verifies that when no payload is specified, the client returns the default empty JSON object "{}". This establishes the expected fallback behavior.

StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs (3)

10-27: LGTM! Excellent test for payload sequencing.

The test thoroughly verifies that multiple payloads are returned in the correct order when multiple requests are made. The fluent API usage with chained With_Payload calls is clean and readable.


29-39: LGTM! Good coverage of default behavior.

The test correctly verifies that when no payloads are configured, the handler returns the default empty JSON object "{}". This ensures consistent behavior across the utility classes.


41-52: LGTM! Comprehensive test for request message capturing.

The test effectively verifies that the handler captures and exposes the HttpRequestMessage that was sent through it. The URI assertion on line 51 correctly shows the expected HttpClient behavior of normalizing "http://foo" to "http://foo/" with a trailing slash.

{D9E0C5BB-8D84-4D5B-90A6-7B091938E6C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9E0C5BB-8D84-4D5B-90A6-7B091938E6C9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobalSection
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove the extra EndGlobalSection statement.

There's an extra EndGlobalSection statement that doesn't correspond to any opening GlobalSection. This could cause parsing issues in Visual Studio.

Apply this diff to fix the structural issue:

-EndGlobalSection

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In TestUtils.sln at line 25, there is an extra EndGlobalSection statement
without a matching opening GlobalSection. Remove this extra EndGlobalSection
line to fix the structural issue and prevent parsing errors in Visual Studio.

Comment on lines +9 to +11
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify package versions for currency and security.

Ensure the test package versions are current and free from known vulnerabilities.


🌐 Web query:

What are the latest versions of Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio NuGet packages for .NET 6.0 projects?

💡 Result:

For .NET 6.0 projects, the latest stable versions of the specified NuGet packages are:

  • Microsoft.NET.Test.Sdk: 17.13.0
  • xunit: 2.9.3
  • xunit.runner.visualstudio: 2.8.2

These versions are compatible with .NET 6.0 and are recommended for use in your testing projects.

To include these packages in your project, you can add the following to your .csproj file:

<ItemGroup>
  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
  <PackageReference Include="xunit" Version="2.9.3" />
  <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

Ensure that your project file targets the correct framework:

<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

After updating your project file, restore the packages using the following command:

dotnet restore

This will download and install the specified versions of the packages, making them available for your testing project.


🏁 Script executed:

#!/bin/bash
# Verify the target framework in the test project
grep -R "<TargetFramework>" -n StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj || echo "No <TargetFramework> tag found in the project file."

Length of output: 137


Update test project package versions to latest stable releases

The test project targets net6.0 (verified), but currently references older package versions. Please update the following in StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj:

• Microsoft.NET.Test.Sdk: 17.6.317.13.0
• xunit: 2.4.22.9.3
• xunit.runner.visualstudio: 2.4.52.8.2

Proposed diff:

<ItemGroup>
-  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
+  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
-  <PackageReference Include="xunit" Version="2.4.2" />
+  <PackageReference Include="xunit" Version="2.9.3" />
-  <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
+  <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

After updating, run:

dotnet restore

to install the new package versions.

🤖 Prompt for AI Agents
In StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj around lines 9 to
11, update the package versions to the latest stable releases compatible with
.NET 6.0. Change Microsoft.NET.Test.Sdk from 17.6.3 to 17.13.0, xunit from 2.4.2
to 2.9.3, and xunit.runner.visualstudio from 2.4.5 to 2.8.2. After modifying
these versions, run 'dotnet restore' to download and install the updated
packages.

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.

2 participants