Skip to content

Commit ede9bad

Browse files
authored
Add ServiceProvider package (#8)
1 parent d894859 commit ede9bad

20 files changed

Lines changed: 517 additions & 4 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ bld/
3434

3535
# Visual Studio 2015/2017 cache/options directory
3636
.vs/
37+
38+
# JetBrains Rider ignores
39+
.idea/
3740
# Uncomment if you have tasks that create the project's static files in wwwroot
3841
#wwwroot/
3942

AutoFixtureExtensions.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extensions", "src\Extension
3939
EndProject
4040
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Extensions", "tests\Tests.Extensions\Tests.Extensions.csproj", "{91F84AD8-6B03-41FD-AA56-860C6A3573D1}"
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceProvider", "src\ServiceProvider\ServiceProvider.csproj", "{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}"
43+
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.ServiceProvider", "tests\Tests.ServiceProvider\Tests.ServiceProvider.csproj", "{ABC56637-1CB8-40AC-ABEE-246DAA635251}"
45+
EndProject
4246
Global
4347
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4448
Debug|Any CPU = Debug|Any CPU
@@ -208,6 +212,30 @@ Global
208212
{91F84AD8-6B03-41FD-AA56-860C6A3573D1}.Release|x64.Build.0 = Release|Any CPU
209213
{91F84AD8-6B03-41FD-AA56-860C6A3573D1}.Release|x86.ActiveCfg = Release|Any CPU
210214
{91F84AD8-6B03-41FD-AA56-860C6A3573D1}.Release|x86.Build.0 = Release|Any CPU
215+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
216+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
217+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|x64.ActiveCfg = Debug|Any CPU
218+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|x64.Build.0 = Debug|Any CPU
219+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|x86.ActiveCfg = Debug|Any CPU
220+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Debug|x86.Build.0 = Debug|Any CPU
221+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
222+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|x64.ActiveCfg = Release|Any CPU
224+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|x64.Build.0 = Release|Any CPU
225+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|x86.ActiveCfg = Release|Any CPU
226+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D}.Release|x86.Build.0 = Release|Any CPU
227+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
228+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|Any CPU.Build.0 = Debug|Any CPU
229+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|x64.ActiveCfg = Debug|Any CPU
230+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|x64.Build.0 = Debug|Any CPU
231+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|x86.ActiveCfg = Debug|Any CPU
232+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Debug|x86.Build.0 = Debug|Any CPU
233+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|Any CPU.ActiveCfg = Release|Any CPU
234+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|Any CPU.Build.0 = Release|Any CPU
235+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|x64.ActiveCfg = Release|Any CPU
236+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|x64.Build.0 = Release|Any CPU
237+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|x86.ActiveCfg = Release|Any CPU
238+
{ABC56637-1CB8-40AC-ABEE-246DAA635251}.Release|x86.Build.0 = Release|Any CPU
211239
EndGlobalSection
212240
GlobalSection(NestedProjects) = preSolution
213241
{9058A266-F629-4774-B1F5-1FF74084F16A} = {18DABDB0-C415-4075-BDC1-53DF4088FA42}
@@ -225,5 +253,7 @@ Global
225253
{01EC83AE-4341-4C80-A648-4E28C517FF3D} = {E7145B79-E211-400B-AA20-9445C38DDA8D}
226254
{E51A9303-87AC-4736-B967-5A687E7F5B1C} = {A8A865D0-D8D6-44BD-B20E-4E80425F1596}
227255
{91F84AD8-6B03-41FD-AA56-860C6A3573D1} = {E7145B79-E211-400B-AA20-9445C38DDA8D}
256+
{CF51D3A8-288F-4FF3-8D16-FF09E1D6FF4D} = {A8A865D0-D8D6-44BD-B20E-4E80425F1596}
257+
{ABC56637-1CB8-40AC-ABEE-246DAA635251} = {E7145B79-E211-400B-AA20-9445C38DDA8D}
228258
EndGlobalSection
229259
EndGlobal

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ The following packages are available:
1313
* **[Kralizek.AutoFixture.Extensions.Grpc](src/Grpc)**<br/>
1414
An integration between AutoFixture and [Grpc.Core.Testing](https://www.nuget.org/packages/Grpc.Core.Testing/) to test GRPC services and components consuming GRPC clients.
1515

16+
* **[Kralizek.AutoFixture.Extensions.ServiceProvider](src/ServiceProvider)**<br/>
17+
An integration between AutoFixture and [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection) to resolve registered services.
18+
1619
## License
1720
The content of this repository is licensed under the [MIT license](https://github.com/Kralizek/AutoFixtureExtensions/blob/master/LICENSE.txt).

global.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sdk": {
3+
"version": "6.0.100",
4+
"rollForward": "latestMinor"
5+
}
6+
}

src/AspNetCore.WebApplicationFactory/AspNetCore.WebApplicationFactory.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="AutoFixture" Version="4.11.0" />
1413
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.*" />
1514
</ItemGroup>
1615

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<RootNamespace>Kralizek.AutoFixture.Extensions</RootNamespace>
5-
<LangVersion>9.0</LangVersion>
5+
<LangVersion>10.0</LangVersion>
66
<Nullable>enable</Nullable>
77
<AssemblyName>$(RootNamespace).$(MSBuildProjectName)</AssemblyName>
88
</PropertyGroup>

src/Grpc/Grpc.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="AutoFixture" Version="4.11.0" />
1413
<PackageReference Include="Grpc.Core.Testing" Version="2.37.0" />
1514
</ItemGroup>
1615

src/MockHttp/MockHttp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="AutoFixture" Version="4.11.0" />
1615
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
1716
</ItemGroup>
1817

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Kralizek.AutoFixture.Extensions;
3+
4+
// ReSharper disable CheckNamespace
5+
6+
namespace AutoFixture;
7+
8+
public static class FixtureExtensions
9+
{
10+
public static IFixture AddServiceProvider(this IFixture fixture, IServiceProvider serviceProvider)
11+
{
12+
_ = fixture ?? throw new ArgumentNullException(nameof(fixture));
13+
14+
_ = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
15+
16+
fixture.Customize(new ServiceProviderCustomization(serviceProvider));
17+
18+
return fixture;
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using AutoFixture.Kernel;
3+
4+
namespace Kralizek.AutoFixture.Extensions.Kernel;
5+
6+
public class ServiceProviderSpecimenBuilder : ISpecimenBuilder
7+
{
8+
private readonly IServiceProvider _serviceProvider;
9+
10+
public ServiceProviderSpecimenBuilder(IServiceProvider serviceProvider)
11+
{
12+
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
13+
}
14+
15+
public object Create(object request, ISpecimenContext context)
16+
{
17+
_ = request ?? throw new ArgumentNullException(nameof(request));
18+
19+
_ = context ?? throw new ArgumentNullException(nameof(context));
20+
21+
if (request is not Type type)
22+
{
23+
return new NoSpecimen();
24+
}
25+
26+
var service = _serviceProvider.GetService(type);
27+
28+
if (service is null)
29+
{
30+
return new NoSpecimen();
31+
}
32+
33+
return service;
34+
}
35+
}

0 commit comments

Comments
 (0)