Skip to content

Commit af85ff8

Browse files
authored
Add support for WebApplicationFactory instances (#11)
1 parent 99b0fe2 commit af85ff8

8 files changed

Lines changed: 487 additions & 23 deletions

src/AspNetCore.WebApplicationFactory/AspNetCoreWebApplicationFactoryFixtureExtensions.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@ public static class AspNetCoreWebApplicationFactoryFixtureExtensions
1313
public static IFixture AddWebApplicationFactorySupport<TEntryPoint>(this IFixture fixture, Action<IWebHostBuilder>? configuration = null)
1414
where TEntryPoint : class
1515
{
16-
return AddWebApplicationFactorySupport<WebApplicationFactory<TEntryPoint>, TEntryPoint>(fixture, configuration ?? EmptyAction);
16+
return AddWebApplicationFactorySupport<WebApplicationFactory<TEntryPoint>, TEntryPoint>(fixture, null, configuration ?? EmptyAction);
17+
}
18+
19+
public static IFixture AddWebApplicationFactorySupport<TEntryPoint>(this IFixture fixture, WebApplicationFactory<TEntryPoint> instance)
20+
where TEntryPoint : class
21+
{
22+
return AddWebApplicationFactorySupport<WebApplicationFactory<TEntryPoint>, TEntryPoint>(fixture, instance, EmptyAction);
1723
}
1824

1925
public static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture)
2026
where TFactory : WebApplicationFactory<TEntryPoint>, new()
2127
where TEntryPoint : class
2228
{
23-
return AddWebApplicationFactorySupport<TFactory, TEntryPoint>(fixture, EmptyAction);
29+
return AddWebApplicationFactorySupport<TFactory, TEntryPoint>(fixture, null, EmptyAction);
30+
}
31+
32+
public static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture, TFactory instance)
33+
where TFactory : WebApplicationFactory<TEntryPoint>, new()
34+
where TEntryPoint : class
35+
{
36+
return AddWebApplicationFactorySupport<TFactory, TEntryPoint>(fixture, instance, EmptyAction);
2437
}
2538

26-
private static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture, Action<IWebHostBuilder> configuration)
39+
private static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(this IFixture fixture, TFactory? instance, Action<IWebHostBuilder> configuration)
2740
where TFactory : WebApplicationFactory<TEntryPoint>, new()
2841
where TEntryPoint : class
2942
{
@@ -32,7 +45,7 @@ private static IFixture AddWebApplicationFactorySupport<TFactory, TEntryPoint>(t
3245
throw new ArgumentNullException(nameof(fixture));
3346
}
3447

35-
fixture.Customize(new WebApplicationFactoryCustomization<TFactory, TEntryPoint>(configuration));
48+
fixture.Customize(new WebApplicationFactoryCustomization<TFactory, TEntryPoint>(instance, configuration));
3649

3750
return fixture;
3851
}

src/AspNetCore.WebApplicationFactory/Internal/WebApplicationFactoryCustomization.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@ public class WebApplicationFactoryCustomization<TFactory, TEntryPoint> : ICustom
99
where TFactory : WebApplicationFactory<TEntryPoint>, new()
1010
where TEntryPoint : class
1111
{
12+
private readonly TFactory? _instance;
1213
private readonly Action<IWebHostBuilder> _configuration;
1314

14-
public WebApplicationFactoryCustomization(Action<IWebHostBuilder> configuration)
15+
public WebApplicationFactoryCustomization(TFactory? instance, Action<IWebHostBuilder> configuration)
1516
{
17+
_instance = instance;
1618
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
1719
}
1820

1921
public void Customize(IFixture fixture)
2022
{
21-
fixture.Inject(new TFactory().WithWebHostBuilder(_configuration));
22-
23+
if (_instance is not null)
24+
{
25+
fixture.Inject<TFactory>(_instance);
26+
}
27+
else if (typeof(TFactory) == typeof(WebApplicationFactory<TEntryPoint>))
28+
{
29+
fixture.Inject(new TFactory().WithWebHostBuilder(_configuration));
30+
}
31+
else
32+
{
33+
fixture.Inject(new TFactory());
34+
}
35+
2336
fixture.Customizations.Add(new HttpClientSpecimenBuilder<TFactory, TEntryPoint>());
2437
}
2538
}

tests/Tests.AspNetCore.WebApplicationFactory/AspNetCoreWebApplicationFactoryFixtureExtensionsTests.cs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,94 @@
22
using Kralizek.AutoFixture.Extensions.Internal;
33
using Microsoft.AspNetCore.Mvc.Testing;
44
using NUnit.Framework;
5+
using TestWebSite;
6+
57
// ReSharper disable InvokeAsExtensionMethod
68

79
namespace Tests
810
{
911
public class AspNetCoreWebApplicationFactoryFixtureExtensionsTests
1012
{
13+
[Test]
14+
public void AddWebApplicationFactorySupport_throws_if_fixture_is_null()
15+
{
16+
Assert.That(() => AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<Startup>(null!), Throws.ArgumentNullException);
17+
}
18+
19+
[Test]
20+
public void AddWebApplicationFactorySupport_with_custom_factory_throws_if_fixture_is_null()
21+
{
22+
Assert.That(() => AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, Startup>(null!), Throws.ArgumentNullException);
23+
}
24+
25+
[Test]
26+
public void AddWebApplicationFactorySupport_with_instance_throws_if_fixture_is_null()
27+
{
28+
var instance = new WebApplicationFactory<Startup>();
29+
30+
Assert.That(() => AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport(null!, instance), Throws.ArgumentNullException);
31+
}
32+
33+
[Test]
34+
public void AddWebApplicationFactorySupport_with_custom_factory_instance_throws_if_fixture_is_null()
35+
{
36+
var instance = new CustomWebApplicationFactory();
37+
38+
Assert.That(() => AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, Startup>(null!, instance), Throws.ArgumentNullException);
39+
}
40+
1141
[Test, CustomAutoData]
1242
public void AddWebApplicationFactorySupport_registers_customization(IFixture fixture)
1343
{
14-
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<TestWebSite.Startup>(fixture);
44+
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<Startup>(fixture);
1545

1646
Assert.Multiple(() =>
1747
{
18-
Assert.That(() => fixture.Create<WebApplicationFactory<TestWebSite.Startup>>(), Throws.Nothing);
48+
Assert.That(() => fixture.Create<WebApplicationFactory<Startup>>(), Throws.Nothing);
1949

20-
Assert.That(fixture.Customizations, Has.Exactly(1).InstanceOf<HttpClientSpecimenBuilder<WebApplicationFactory<TestWebSite.Startup>, TestWebSite.Startup>>());
50+
Assert.That(fixture.Customizations, Has.Exactly(1).InstanceOf<HttpClientSpecimenBuilder<WebApplicationFactory<Startup>, Startup>>());
2151
});
2252
}
2353

2454
[Test, CustomAutoData]
2555
public void AddWebApplicationFactorySupport_registers_customization_with_custom_factory(IFixture fixture)
2656
{
27-
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, TestWebSite.Startup>(fixture);
57+
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, Startup>(fixture);
58+
59+
Assert.Multiple(() =>
60+
{
61+
Assert.That(() => fixture.Create<CustomWebApplicationFactory>(), Throws.Nothing);
62+
63+
Assert.That(fixture.Customizations, Has.Exactly(1).InstanceOf<HttpClientSpecimenBuilder<CustomWebApplicationFactory, Startup>>());
64+
});
65+
}
66+
67+
[Test, CustomAutoData]
68+
public void AddWebApplicationFactorySupport_registers_customization_with_instance(IFixture fixture)
69+
{
70+
var instance = new CustomWebApplicationFactory();
71+
72+
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, Startup>(fixture, instance);
2873

2974
Assert.Multiple(() =>
3075
{
76+
3177
Assert.That(() => fixture.Create<CustomWebApplicationFactory>(), Throws.Nothing);
3278

33-
Assert.That(fixture.Customizations, Has.Exactly(1).InstanceOf<HttpClientSpecimenBuilder<CustomWebApplicationFactory, TestWebSite.Startup>>());
79+
Assert.That(fixture.Customizations, Has.Exactly(1).InstanceOf<HttpClientSpecimenBuilder<CustomWebApplicationFactory, Startup>>());
3480
});
3581
}
82+
83+
[Test, CustomAutoData]
84+
public void AddWebApplicationFactorySupport_returns_registered_instance(IFixture fixture)
85+
{
86+
var instance = new CustomWebApplicationFactory();
87+
88+
AspNetCoreWebApplicationFactoryFixtureExtensions.AddWebApplicationFactorySupport<CustomWebApplicationFactory, Startup>(fixture, instance);
89+
90+
var factory = fixture.Create<CustomWebApplicationFactory>();
91+
92+
Assert.That(factory, Is.SameAs(instance));
93+
}
3694
}
3795
}

tests/Tests.AspNetCore.WebApplicationFactory/Integration/CustomWebFactoryIntegrationTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading.Tasks;
55
using AutoFixture;
66
using AutoFixture.NUnit3;
7-
using Microsoft.AspNetCore.TestHost;
87
using NUnit.Framework;
98

109
namespace Tests.Integration
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using AutoFixture;
6+
using AutoFixture.NUnit3;
7+
using Microsoft.AspNetCore.TestHost;
8+
using NUnit.Framework;
9+
10+
namespace Tests.Integration
11+
{
12+
[TestFixture]
13+
public class InstanceCustomWebFactoryIntegrationTests
14+
{
15+
[AttributeUsage(AttributeTargets.Method)]
16+
public class TestAutoDataAttribute : AutoDataAttribute
17+
{
18+
public TestAutoDataAttribute() : base(() => new Fixture().AddWebApplicationFactorySupport<CustomWebApplicationFactory, TestWebSite.Startup>(new CustomWebApplicationFactory())) { }
19+
}
20+
21+
[Test]
22+
public async Task Should_work_with_no_auto_data_attribute()
23+
{
24+
var instance = new CustomWebApplicationFactory();
25+
26+
var fixture = new Fixture().AddWebApplicationFactorySupport<CustomWebApplicationFactory, TestWebSite.Startup>(instance);
27+
28+
var message = fixture.Create<string>();
29+
30+
var http = fixture.Create<HttpClient>();
31+
32+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
33+
34+
response.EnsureSuccessStatusCode();
35+
36+
var responseContent = await response.Content.ReadAsStringAsync();
37+
38+
Assert.That(responseContent, Is.EqualTo(message));
39+
}
40+
41+
[Test, AutoData]
42+
public async Task Should_work_with_basic_auto_data_attribute(IFixture fixture, string message)
43+
{
44+
var instance = new CustomWebApplicationFactory();
45+
46+
fixture.AddWebApplicationFactorySupport<CustomWebApplicationFactory, TestWebSite.Startup>(instance);
47+
48+
var http = fixture.Create<HttpClient>();
49+
50+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
51+
52+
response.EnsureSuccessStatusCode();
53+
54+
var responseContent = await response.Content.ReadAsStringAsync();
55+
56+
Assert.That(responseContent, Is.EqualTo(message));
57+
}
58+
59+
[Test, TestAutoData]
60+
public async Task Should_work_with_custom_auto_data_attribute(HttpClient http, string message)
61+
{
62+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
63+
64+
response.EnsureSuccessStatusCode();
65+
66+
var responseContent = await response.Content.ReadAsStringAsync();
67+
68+
Assert.That(responseContent, Is.EqualTo(message));
69+
}
70+
}
71+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using AutoFixture;
6+
using AutoFixture.NUnit3;
7+
using Microsoft.AspNetCore.Mvc.Testing;
8+
using Microsoft.AspNetCore.TestHost;
9+
using NUnit.Framework;
10+
using TestWebSite;
11+
12+
namespace Tests.Integration
13+
{
14+
[TestFixture]
15+
public class InstanceStandardWebFactoryIntegrationTests
16+
{
17+
[AttributeUsage(AttributeTargets.Method)]
18+
public class TestAutoDataAttribute : AutoDataAttribute
19+
{
20+
public TestAutoDataAttribute() : base(() => new Fixture().AddWebApplicationFactorySupport(new WebApplicationFactory<Startup>())) { }
21+
}
22+
23+
[Test]
24+
public async Task Should_work_with_no_auto_data_attribute()
25+
{
26+
var instance = new WebApplicationFactory<Startup>();
27+
28+
var fixture = new Fixture().AddWebApplicationFactorySupport(instance);
29+
30+
var message = fixture.Create<string>();
31+
32+
var http = fixture.Create<HttpClient>();
33+
34+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
35+
36+
response.EnsureSuccessStatusCode();
37+
38+
var responseContent = await response.Content.ReadAsStringAsync();
39+
40+
Assert.That(responseContent, Is.EqualTo(message));
41+
}
42+
43+
[Test, AutoData]
44+
public async Task Should_work_with_basic_auto_data_attribute(IFixture fixture, string message)
45+
{
46+
var instance = new WebApplicationFactory<Startup>();
47+
48+
fixture.AddWebApplicationFactorySupport(instance);
49+
50+
var http = fixture.Create<HttpClient>();
51+
52+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
53+
54+
response.EnsureSuccessStatusCode();
55+
56+
var responseContent = await response.Content.ReadAsStringAsync();
57+
58+
Assert.That(responseContent, Is.EqualTo(message));
59+
}
60+
61+
[Test, TestAutoData]
62+
public async Task Should_work_with_custom_auto_data_attribute(HttpClient http, string message)
63+
{
64+
var response = await http.PostAsync("/", new FormUrlEncodedContent(new Dictionary<string, string> { ["message"] = message }));
65+
66+
response.EnsureSuccessStatusCode();
67+
68+
var responseContent = await response.Content.ReadAsStringAsync();
69+
70+
Assert.That(responseContent, Is.EqualTo(message));
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)