diff --git a/CHANGELOG.md b/CHANGELOG.md index 603269a..e7ae2a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,5 @@ 2025-05-18 Added unit tests for InternalExtensions - Build failed: unable to restore NuGet packages offline + +2025-05-19 Added tests for ServiceCollectionExtensions + - Fixed build and enabled unit tests \ No newline at end of file diff --git a/DevDistricts.Tests/DevDistricts.Tests.csproj b/DevDistricts.Tests/DevDistricts.Tests.csproj index a7eec33..8c30adf 100644 --- a/DevDistricts.Tests/DevDistricts.Tests.csproj +++ b/DevDistricts.Tests/DevDistricts.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/DevDistricts.Tests/ServiceCollectionExtensionsTests.cs b/DevDistricts.Tests/ServiceCollectionExtensionsTests.cs new file mode 100644 index 0000000..e2e9d3c --- /dev/null +++ b/DevDistricts.Tests/ServiceCollectionExtensionsTests.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using DevDistricts; +using DevDistricts.Internal; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Xunit; + +[District(DependencyInjectionTypes = new[] { typeof(SampleService) })] +[Occupant(UserName = "user", MachineName = "machine")] +internal class SampleDistrict +{ +} + +internal class SampleService +{ +} + +public class ServiceCollectionExtensionsTests +{ + [Fact] + public void WithDevDistricts_RegistersHostedService_AndInvokesCallback() + { + var services = new ServiceCollection(); + Type[]? callbackTypes = null; + + services.WithDevDistricts(o => o + .WithUserName("user") + .WithMachineName("machine") + .WithDependencyInjectionCallback(types => callbackTypes = types.ToArray())); + + var descriptor = services.Single(d => d.ServiceType == typeof(IHostedService)); + Assert.Equal(typeof(DistrictRunner), descriptor.ImplementationType); + Assert.NotNull(callbackTypes); + Assert.Contains(typeof(SampleService), callbackTypes); + } + + [Fact] + public void WithDevDistricts_Throws_NoMatchingDistrictException_When_No_District() + { + var services = new ServiceCollection(); + Assert.Throws(() => + services.WithDevDistricts(o => o + .WithUserName("none") + .WithMachineName("none"))); + } + + [Fact] + public void WithDevDistricts_Throws_NotSupported_When_DITypes_NoCallback() + { + var services = new ServiceCollection(); + Assert.Throws(() => + services.WithDevDistricts(o => o + .WithUserName("user") + .WithMachineName("machine"))); + } +} diff --git a/DevDistricts.sln.DotSettings.user b/DevDistricts.sln.DotSettings.user new file mode 100644 index 0000000..7930c35 --- /dev/null +++ b/DevDistricts.sln.DotSettings.user @@ -0,0 +1,4 @@ + + <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;DevDistricts.Tests&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Project Location="E:\Projects\DevDistricts\DevDistricts.Tests" Presentation="&lt;DevDistricts.Tests&gt;" /> +</SessionState> \ No newline at end of file diff --git a/DevDistricts/AssemblyInfo.cs b/DevDistricts/AssemblyInfo.cs new file mode 100644 index 0000000..e10192a --- /dev/null +++ b/DevDistricts/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("DevDistricts.Tests")] diff --git a/DevDistricts/DevDistricts.csproj b/DevDistricts/DevDistricts.csproj index dea3c0a..8ca62f4 100644 --- a/DevDistricts/DevDistricts.csproj +++ b/DevDistricts/DevDistricts.csproj @@ -2,6 +2,7 @@ netstandard2.0 + latestMajor enable https://github.com/akrock/DevDistricts/blob/master/LICENSE https://github.com/akrock/DevDistricts