diff --git a/StoneAge.TestUtils.Tests/MockLoggerTests.cs b/StoneAge.TestUtils.Tests/MockLoggerTests.cs new file mode 100644 index 0000000..4ceda1c --- /dev/null +++ b/StoneAge.TestUtils.Tests/MockLoggerTests.cs @@ -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(); + 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(); + Assert.True(logger.IsEnabled(LogLevel.Debug)); + Assert.True(logger.IsEnabled(LogLevel.Warning)); + } + + [Fact] + public void Fetch_Entries_For_ReturnsEntries() + { + var logger = new MockLogger(); + 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(); + Assert.Throws(() => logger.BeginScope("scope")); + } + } +} \ No newline at end of file diff --git a/StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs b/StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs new file mode 100644 index 0000000..0094bd6 --- /dev/null +++ b/StoneAge.TestUtils.Tests/MockOptionsSnapshotTests.cs @@ -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); + + 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); + + Assert.Throws(() => snapshot.Get("any")); + } + } +} \ No newline at end of file diff --git a/StoneAge.TestUtils.Tests/MockOptionsTests.cs b/StoneAge.TestUtils.Tests/MockOptionsTests.cs new file mode 100644 index 0000000..3bb7bc2 --- /dev/null +++ b/StoneAge.TestUtils.Tests/MockOptionsTests.cs @@ -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); + + Assert.Equal("Test", options.Value.Name); + Assert.Same(settings, options.Value); + } + } +} \ No newline at end of file diff --git a/StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj b/StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj new file mode 100644 index 0000000..bfba4dd --- /dev/null +++ b/StoneAge.TestUtils.Tests/StoneAge.TestUtils.Tests.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + false + + + + + + + + + + + + + \ No newline at end of file diff --git a/StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs b/StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs new file mode 100644 index 0000000..1fc47bc --- /dev/null +++ b/StoneAge.TestUtils.Tests/TestHttpClientBuilderTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs b/StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs new file mode 100644 index 0000000..0c9bc85 --- /dev/null +++ b/StoneAge.TestUtils.Tests/TestHttpMessageHandlerTests.cs @@ -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()); + } + } +} \ No newline at end of file diff --git a/TestUtils.sln b/TestUtils.sln index a8125a3..c579abb 100644 --- a/TestUtils.sln +++ b/TestUtils.sln @@ -1,9 +1,11 @@  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 @@ -11,11 +13,16 @@ Global 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection