-
Notifications
You must be signed in to change notification settings - Fork 0
Add Unit Tests for Mock Logger and Options Classes #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.Extensions.Logging; | ||
| using StoneAge.TestUtils; | ||
| using Xunit; | ||
|
|
||
| namespace StoneAge.TestUtils.Tests | ||
| { | ||
| public class MockLoggerTests | ||
| { | ||
| [Fact] | ||
| public void Log_AddsEntry_ForGivenLogLevel() | ||
| { | ||
| var logger = new MockLogger<string>(); | ||
| logger.Log(LogLevel.Information, new EventId(), "Test message", null, (s, e) => s); | ||
|
|
||
| Assert.Contains("Test message", logger.LogEntries[LogLevel.Information]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsEnabled_AlwaysReturnsTrue() | ||
| { | ||
| var logger = new MockLogger<string>(); | ||
| Assert.True(logger.IsEnabled(LogLevel.Debug)); | ||
| Assert.True(logger.IsEnabled(LogLevel.Warning)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Fetch_Entries_For_ReturnsEntries() | ||
| { | ||
| var logger = new MockLogger<string>(); | ||
| logger.Log(LogLevel.Error, new EventId(), "Error logged", null, (s, e) => s); | ||
|
|
||
| var entries = logger.Fetch_Entries_For(LogLevel.Error); | ||
| Assert.Single(entries); | ||
| Assert.Equal("Error logged", entries.First()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BeginScope_ThrowsNotImplementedException() | ||
| { | ||
| var logger = new MockLogger<string>(); | ||
| Assert.Throws<NotImplementedException>(() => logger.BeginScope("scope")); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System; | ||
| using StoneAge.TestUtils; | ||
| using Xunit; | ||
|
|
||
| namespace StoneAge.TestUtils.Tests | ||
| { | ||
| public class MockOptionsSnapshotTests | ||
| { | ||
| private class Config | ||
| { | ||
| public int Value { get; set; } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Value_ReturnsInjectedInstance() | ||
| { | ||
| var config = new Config { Value = 123 }; | ||
| var snapshot = new MockOptionsSnapshot<Config>(config); | ||
|
|
||
| Assert.Equal(123, snapshot.Value.Value); | ||
| Assert.Same(config, snapshot.Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Get_ThrowsNotImplementedException() | ||
| { | ||
| var config = new Config(); | ||
| var snapshot = new MockOptionsSnapshot<Config>(config); | ||
|
|
||
| Assert.Throws<NotImplementedException>(() => snapshot.Get("any")); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using StoneAge.TestUtils; | ||
| using Xunit; | ||
|
|
||
| namespace StoneAge.TestUtils.Tests | ||
| { | ||
| public class MockOptionsTests | ||
| { | ||
| private class Settings | ||
| { | ||
| public string Name { get; set; } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Value_ReturnsInjectedInstance() | ||
| { | ||
| var settings = new Settings { Name = "Test" }; | ||
| var options = new MockOptions<Settings>(settings); | ||
|
|
||
| Assert.Equal("Test", options.Value.Name); | ||
| Assert.Same(settings, options.Value); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <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" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TestUtils\StoneAge.TestUtils.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
| using StoneAge.TestUtils; | ||
| using Xunit; | ||
|
|
||
| namespace StoneAge.TestUtils.Tests | ||
| { | ||
| public class TestHttpClientBuilderTests | ||
| { | ||
| [Fact] | ||
| public async Task Create_ReturnsClient_ThatReturnsPayload() | ||
| { | ||
| var builder = new TestHttpClientBuilder().With_Payload("{\"foo\":\"bar\"}"); | ||
| var (client, handler) = builder.Create(); | ||
|
|
||
| var response = await client.GetAsync("http://test"); | ||
| var content = await response.Content.ReadAsStringAsync(); | ||
|
|
||
| Assert.Equal("{\"foo\":\"bar\"}", content); | ||
| Assert.NotNull(handler.RequestMessage); | ||
| Assert.Equal("http://test/", handler.RequestMessage.RequestUri.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Create_ReturnsClient_WithDefaultPayload() | ||
| { | ||
| var builder = new TestHttpClientBuilder(); | ||
| var (client, handler) = builder.Create(); | ||
|
|
||
| var response = await client.GetAsync("http://test"); | ||
| var content = await response.Content.ReadAsStringAsync(); | ||
|
|
||
| Assert.Equal("{}", content); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
| using StoneAge.TestUtils; | ||
| using Xunit; | ||
|
|
||
| namespace StoneAge.TestUtils.Tests | ||
| { | ||
| public class TestHttpMessageHandlerTests | ||
| { | ||
| [Fact] | ||
| public async Task SendAsync_ReturnsSpecifiedPayloadsInOrder() | ||
| { | ||
| var handler = new TestHttpMessageHandler() | ||
| .With_Payload("first") | ||
| .With_Payload("second"); | ||
|
|
||
| var client = new HttpClient(handler); | ||
|
|
||
| var resp1 = await client.GetAsync("http://one"); | ||
| var resp2 = await client.GetAsync("http://two"); | ||
|
|
||
| var content1 = await resp1.Content.ReadAsStringAsync(); | ||
| var content2 = await resp2.Content.ReadAsStringAsync(); | ||
|
|
||
| Assert.Equal("first", content1); | ||
| Assert.Equal("second", content2); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SendAsync_ReturnsDefaultPayload_WhenNoneSpecified() | ||
| { | ||
| var handler = new TestHttpMessageHandler(); | ||
| var client = new HttpClient(handler); | ||
|
|
||
| var resp = await client.GetAsync("http://none"); | ||
| var content = await resp.Content.ReadAsStringAsync(); | ||
|
|
||
| Assert.Equal("{}", content); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SendAsync_Sets_RequestMessage() | ||
| { | ||
| var handler = new TestHttpMessageHandler().With_Payload("payload"); | ||
| var client = new HttpClient(handler); | ||
|
|
||
| var req = new HttpRequestMessage(HttpMethod.Get, "http://foo"); | ||
| await client.SendAsync(req); | ||
|
|
||
| Assert.NotNull(handler.RequestMessage); | ||
| Assert.Equal("http://foo/", handler.RequestMessage.RequestUri.ToString()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,28 @@ | ||
|
|
||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.29009.5 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.6.33804.203 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StoneAge.TestUtils", "TestUtils\StoneAge.TestUtils.csproj", "{5F82CAB0-84CF-43F2-8572-27A74D56DF20}" | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StoneAge.TestUtils", "TestUtils\StoneAge.TestUtils.csproj", "{C1E0C5AA-8D84-4D5B-90A6-7B091938E6C5}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StoneAge.TestUtils.Tests", "StoneAge.TestUtils.Tests\StoneAge.TestUtils.Tests.csproj", "{D9E0C5BB-8D84-4D5B-90A6-7B091938E6C9}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {C1E0C5AA-8D84-4D5B-90A6-7B091938E6C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {C1E0C5AA-8D84-4D5B-90A6-7B091938E6C5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {C1E0C5AA-8D84-4D5B-90A6-7B091938E6C5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {C1E0C5AA-8D84-4D5B-90A6-7B091938E6C5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {D9E0C5BB-8D84-4D5B-90A6-7B091938E6C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {D9E0C5BB-8D84-4D5B-90A6-7B091938E6C9}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extra There's an extra Apply this diff to fix the structural issue: -EndGlobalSection
🤖 Prompt for AI Agents |
||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
|
|
||
There was a problem hiding this comment.
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:
💡 Result:
For .NET 6.0 projects, the latest stable versions of the specified NuGet packages are:
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
.csprojfile:Ensure that your project file targets the correct framework:
After updating your project file, restore the packages using the following command:
This will download and install the specified versions of the packages, making them available for your testing project.
🏁 Script executed:
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.3 → 17.13.0
• xunit: 2.4.2 → 2.9.3
• xunit.runner.visualstudio: 2.4.5 → 2.8.2
Proposed diff:
After updating, run:
to install the new package versions.
🤖 Prompt for AI Agents