Skip to content

Commit 99b0fe2

Browse files
authored
Add support for classes WebApplicationFactory subclasses (#10)
* Added support for classes inheriting from WebApplicationFactory * Switched all projects to support .NET 6.0 and .NET 7.0
1 parent adc8431 commit 99b0fe2

25 files changed

Lines changed: 156 additions & 46 deletions

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "6.0.100",
3+
"version": "7.0.100",
44
"rollForward": "latestMinor"
55
}
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
</Project>

helpers/Grpc/TestGrpcClient/TestGrpcClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

helpers/Grpc/TestGrpcService/TestGrpcService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup>
@@ -10,7 +10,9 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.*" />
13+
<PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.*" />
14+
<PackageReference Condition="'$(TargetFramework)' == 'net7.0'" Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.*" />
15+
1416
</ItemGroup>
1517

1618
</Project>

src/AspNetCore.WebApplicationFactory/AspNetCoreWebApplicationFactoryFixtureExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Kralizek.AutoFixture.Extensions.Internal;
33
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Testing;
45
// ReSharper disable CheckNamespace
56

67
namespace AutoFixture
@@ -11,13 +12,27 @@ public static class AspNetCoreWebApplicationFactoryFixtureExtensions
1112

1213
public static IFixture AddWebApplicationFactorySupport<TEntryPoint>(this IFixture fixture, Action<IWebHostBuilder>? configuration = null)
1314
where TEntryPoint : class
15+
{
16+
return AddWebApplicationFactorySupport<WebApplicationFactory<TEntryPoint>, TEntryPoint>(fixture, configuration ?? EmptyAction);
17+
}
18+
19+
public static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture)
20+
where TFactory : WebApplicationFactory<TEntryPoint>, new()
21+
where TEntryPoint : class
22+
{
23+
return AddWebApplicationFactorySupport<TFactory, TEntryPoint>(fixture, EmptyAction);
24+
}
25+
26+
private static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture, Action<IWebHostBuilder> configuration)
27+
where TFactory : WebApplicationFactory<TEntryPoint>, new()
28+
where TEntryPoint : class
1429
{
1530
if (fixture is null)
1631
{
1732
throw new ArgumentNullException(nameof(fixture));
1833
}
1934

20-
fixture.Customize(new WebApplicationFactoryCustomization<TEntryPoint>(configuration ?? EmptyAction));
35+
fixture.Customize(new WebApplicationFactoryCustomization<TFactory, TEntryPoint>(configuration));
2136

2237
return fixture;
2338
}

src/AspNetCore.WebApplicationFactory/Internal/HttpClientSpecimenBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace Kralizek.AutoFixture.Extensions.Internal
77
{
8-
public class HttpClientSpecimenBuilder<TEntryPoint> : ISpecimenBuilder
8+
public class HttpClientSpecimenBuilder<TFactory, TEntryPoint> : ISpecimenBuilder
9+
where TFactory : WebApplicationFactory<TEntryPoint>
910
where TEntryPoint : class
1011
{
1112
public HttpClientSpecimenBuilder(IRequestSpecification requestSpecification)
@@ -34,7 +35,7 @@ public object Create(object request, ISpecimenContext context)
3435
return new NoSpecimen();
3536
}
3637

37-
var factory = context.Create<WebApplicationFactory<TEntryPoint>>();
38+
var factory = context.Create<TFactory>();
3839

3940
return factory.CreateClient();
4041
}

src/AspNetCore.WebApplicationFactory/Internal/WebApplicationFactoryCustomization.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace Kralizek.AutoFixture.Extensions.Internal
77
{
8-
public class WebApplicationFactoryCustomization<TEntryPoint> : ICustomization
8+
public class WebApplicationFactoryCustomization<TFactory, TEntryPoint> : ICustomization
9+
where TFactory : WebApplicationFactory<TEntryPoint>, new()
910
where TEntryPoint : class
1011
{
1112
private readonly Action<IWebHostBuilder> _configuration;
@@ -17,9 +18,9 @@ public WebApplicationFactoryCustomization(Action<IWebHostBuilder> configuration)
1718

1819
public void Customize(IFixture fixture)
1920
{
20-
fixture.Inject(new WebApplicationFactory<TEntryPoint>().WithWebHostBuilder(_configuration));
21+
fixture.Inject(new TFactory().WithWebHostBuilder(_configuration));
2122

22-
fixture.Customizations.Add(new HttpClientSpecimenBuilder<TEntryPoint>());
23+
fixture.Customizations.Add(new HttpClientSpecimenBuilder<TFactory, TEntryPoint>());
2324
}
2425
}
2526
}

src/Extensions/Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
<RootNamespace>Kralizek.AutoFixture.Extensions</RootNamespace>
66
<LangVersion>9.0</LangVersion>
77
<Nullable>enable</Nullable>

src/Grpc/Grpc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup>

0 commit comments

Comments
 (0)