Skip to content

Commit c7caaf5

Browse files
Improve service collection extensions and add test
1 parent 21fc37c commit c7caaf5

File tree

7 files changed

+78
-25
lines changed

7 files changed

+78
-25
lines changed

src/IT.Web.Common.Abstractions/IT.Web.Common.Abstractions.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<None Include="..\..\docs\README.md" Pack="true" PackagePath="\"/>
17+
<None Include="..\..\docs\README.md" Pack="true" PackagePath="\" Visible="false"/>
1818
</ItemGroup>
1919

2020
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using IT.Web.Common.Abstractions;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
34

45
namespace IT.Web.Common.Extensions;
56

67
public static class ServiceCollectionExtensions
78
{
8-
public static IServiceCollection RegisterCommonWebServices(this IServiceCollection services)
9+
/// <summary>
10+
/// Add the IT.Web.Common services to the application
11+
/// </summary>
12+
/// <param name="services">Collection of service descriptors</param>
13+
public static void AddItWebCommonServices(this IServiceCollection services)
914
{
10-
services.AddSingleton<IWebAppFileProvider, WebAppFileProvider>();
11-
services.AddSingleton<ITypeFinder, WebAppTypeFinder>();
12-
13-
return services;
15+
services.TryAddSingleton<IWebAppFileProvider, WebAppFileProvider>();
16+
services.TryAddSingleton<ITypeFinder, WebAppTypeFinder>();
1417
}
1518
}

src/IT.Web.Common/IT.Web.Common.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<None Include="..\..\docs\README.md" Pack="true" PackagePath="\"/>
17+
<None Include="..\..\docs\README.md" Pack="true" PackagePath="\" Visible="false"/>
1818
</ItemGroup>
1919

2020
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using IT.Web.Common.Extensions;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace IT.Web.Common.Tests.Fixtures;
5+
6+
public class ServiceCollectionExtensionsFixture
7+
{
8+
public IServiceProvider Services => _host.Services;
9+
private readonly IHost _host;
10+
11+
public ServiceCollectionExtensionsFixture()
12+
{
13+
IHostBuilder hostBuilder = new HostBuilder();
14+
15+
hostBuilder.ConfigureServices(services => services.AddItWebCommonServices());
16+
17+
_host = hostBuilder.Build();
18+
}
19+
}

tests/IT.Web.Common.Tests/IT.Web.Common.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="FluentAssertions" Version="6.7.0" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
1415
<PackageReference Include="xunit" Version="2.4.1" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using IT.Web.Common.Abstractions;
2+
using IT.Web.Common.Tests.Fixtures;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
6+
namespace IT.Web.Common.Tests;
7+
8+
public class ServiceCollectionExtensionsTests
9+
{
10+
private readonly ServiceCollectionExtensionsFixture _fixture = new();
11+
12+
[Fact]
13+
public void RegistersTheAppropriateServices()
14+
{
15+
IHostEnvironment? hostEnvironment = _fixture.Services.GetService<IHostEnvironment>();
16+
IWebAppFileProvider? webAppFileProvider = _fixture.Services.GetService<IWebAppFileProvider>();
17+
ITypeFinder? typeFinder = _fixture.Services.GetService<ITypeFinder>();
18+
19+
Assert.NotNull(hostEnvironment);
20+
Assert.NotNull(webAppFileProvider);
21+
Assert.NotNull(typeFinder);
22+
23+
Assert.IsType<WebAppFileProvider>(webAppFileProvider);
24+
Assert.IsType<WebAppTypeFinder>(typeFinder);
25+
}
26+
}

tests/IT.Web.Common.Tests/WebAppTypeFinderTests.cs

+22-18
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,42 @@
33

44
namespace IT.Web.Common.Tests;
55

6-
public class UnitTest1
6+
public class WebAppTypeFinderTests
77
{
88
[Fact]
9-
public void Return()
9+
public void FindClassesOfType_ReturnsTheAppropriateTypes()
1010
{
1111
ITypeFinder finder = new WebAppTypeFinder();
1212

13-
List<Type> findableInterfaceType = finder.FindClassesOfType<IMyInterface>().ToList();
14-
findableInterfaceType.Count.Should().Be(1);
13+
Type[] findableInterfaceType = finder.FindClassesOfType<IMyInterface>().ToArray();
14+
findableInterfaceType.Length.Should().Be(1);
1515
typeof(IMyInterface).IsAssignableFrom(findableInterfaceType.FirstOrDefault()).Should().BeTrue();
1616

17-
List<Type> findableClassType = finder.FindClassesOfType<MyFindableClass>().ToList();
18-
findableClassType.Count.Should().Be(1);
17+
Type[] findableClassType = finder.FindClassesOfType<MyFindableClass>().ToArray();
18+
findableClassType.Length.Should().Be(1);
1919
typeof(MyFindableClass).IsAssignableFrom(findableClassType.FirstOrDefault()).Should().BeTrue();
2020

21-
List<Type> ignoredType = finder.FindClassesOfType<MyIgnoredClass>().ToList();
22-
ignoredType.Count.Should().Be(0);
21+
Type[] ignoredType = finder.FindClassesOfType<MyIgnoredClass>().ToArray();
22+
ignoredType.Length.Should().Be(0);
2323
}
24-
}
2524

26-
public interface IMyInterface
27-
{
25+
// ReSharper disable once MemberCanBePrivate.Global
26+
public interface IMyInterface
27+
{
2828

29-
}
29+
}
3030

31-
public class MyFindableClass : IMyInterface
32-
{
31+
// ReSharper disable once MemberCanBePrivate.Global
32+
public class MyFindableClass : IMyInterface
33+
{
3334

34-
}
35+
}
3536

36-
[ReflectionIgnore]
37-
public class MyIgnoredClass : IMyInterface
38-
{
37+
[ReflectionIgnore]
38+
// ReSharper disable once MemberCanBePrivate.Global
39+
// ReSharper disable once ClassNeverInstantiated.Global
40+
public class MyIgnoredClass : IMyInterface
41+
{
3942

43+
}
4044
}

0 commit comments

Comments
 (0)