Skip to content

Commit d95116c

Browse files
committed
Ensuring factory overrides for Builder<T> work
1 parent 76264c4 commit d95116c

File tree

8 files changed

+63
-8
lines changed

8 files changed

+63
-8
lines changed

TestStack.Dossier.Tests/Builder_CreateListTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Linq;
44
using Shouldly;
55
using TestStack.Dossier.DataSources.Generators;
6+
using TestStack.Dossier.Factories;
67
using TestStack.Dossier.Lists;
8+
using TestStack.Dossier.Tests.Stubs.Examples;
79
using TestStack.Dossier.Tests.Stubs.ViewModels;
810
using Xunit;
911

@@ -167,5 +169,40 @@ public void WhenBuildingObjectsImplicitly_ThenTheAnonymousValueFixtureIsSharedAc
167169

168170
studentViewModels.Select(x => x.Grade).ShouldBeUnique();
169171
}
172+
173+
public void WhenBuildingObjectsWithCtorAndPrivateSetters_ShouldSetPrivateSettersByDefault()
174+
{
175+
var dto = Builder<MixedAccessibilityDto>.CreateListOfSize(1)
176+
.TheFirst(1)
177+
.Set(x => x.SetByCtorWithPublicSetter, "1")
178+
.Set(x => x.SetByCtorWithPrivateSetter, "2")
179+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
180+
.Set(x => x.NotSetByCtorWithPublicSetter, "4")
181+
.BuildList()
182+
[0];
183+
184+
dto.SetByCtorWithPublicSetter.ShouldBe("1");
185+
dto.SetByCtorWithPrivateSetter.ShouldBe("2");
186+
dto.NotSetByCtorWithPrivateSetter.ShouldBe("3");
187+
dto.NotSetByCtorWithPublicSetter.ShouldBe("4");
188+
}
189+
190+
[Fact]
191+
public void GivenBuilderListWithFactoryOverride_WhenBuildingObjects_ShouldRespectOverriddenFactory()
192+
{
193+
var dto = Builder<MixedAccessibilityDto>.CreateListOfSize(1, new CallConstructorFactory())
194+
.TheFirst(1)
195+
.Set(x => x.SetByCtorWithPublicSetter, "1")
196+
.Set(x => x.SetByCtorWithPrivateSetter, "2")
197+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
198+
.Set(x => x.NotSetByCtorWithPublicSetter, "4")
199+
.BuildList()
200+
[0];
201+
202+
dto.SetByCtorWithPublicSetter.ShouldBe("1");
203+
dto.SetByCtorWithPrivateSetter.ShouldBe("2");
204+
dto.NotSetByCtorWithPrivateSetter.ShouldNotBe("3");
205+
dto.NotSetByCtorWithPublicSetter.ShouldNotBe("4");
206+
}
170207
}
171208
}

TestStack.Dossier.Tests/Builder_CreateNewTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Shouldly;
3+
using TestStack.Dossier.Factories;
34
using TestStack.Dossier.Tests.Stubs.Entities;
45
using TestStack.Dossier.Tests.Stubs.Examples;
56
using TestStack.Dossier.Tests.Stubs.ViewModels;
@@ -70,5 +71,20 @@ public void GivenBuilder_WhenBuildingObjectWithCtorAndPrivateSetters_ShouldSetPr
7071
dto.NotSetByCtorWithPrivateSetter.ShouldBe("3");
7172
dto.NotSetByCtorWithPublicSetter.ShouldBe("4");
7273
}
74+
75+
[Fact]
76+
public void GivenBuilderWithFactoryOverride_WhenBuildingObject_ShouldRespectOverriddenFactory()
77+
{
78+
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>.CreateNew(new CallConstructorFactory())
79+
.Set(x => x.SetByCtorWithPublicSetter, "1")
80+
.Set(x => x.SetByCtorWithPrivateSetter, "2")
81+
.Set(x => x.NotSetByCtorWithPrivateSetter, "3")
82+
.Set(x => x.NotSetByCtorWithPublicSetter, "4");
83+
84+
dto.SetByCtorWithPublicSetter.ShouldBe("1");
85+
dto.SetByCtorWithPrivateSetter.ShouldBe("2");
86+
dto.NotSetByCtorWithPrivateSetter.ShouldNotBe("3");
87+
dto.NotSetByCtorWithPublicSetter.ShouldNotBe("4");
88+
}
7389
}
7490
}

TestStack.Dossier.Tests/Builders/AutoConstructorCustomerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AutoConstructorCustomerBuilder WhoJoinedIn(int year)
2222

2323
protected override Customer BuildObject()
2424
{
25-
return BuildUsing<ConstructorFactory>();
25+
return BuildUsing<CallConstructorFactory>();
2626
}
2727
}
2828
}

TestStack.Dossier.Tests/Factories/ConstructorFactoryTests.cs renamed to TestStack.Dossier.Tests/Factories/CallConstructorFactoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
namespace TestStack.Dossier.Tests.Factories
88
{
9-
public class ConstructorFactoryTests
9+
public class CallConstructorFactoryTests
1010
{
1111
[Fact]
1212
public void GivenConstructorFactory_WhenBuilding_ThenOnlyConstructorPropertiesSet()
1313
{
14-
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>.CreateNew(new ConstructorFactory());
14+
MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>.CreateNew(new CallConstructorFactory());
1515

1616
// ctor properties
1717
dto.SetByCtorWithPrivateSetter.ShouldNotBe(null);

TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<Compile Include="ChildBuilderTests.cs" />
6262
<Compile Include="Factories\AllPropertiesFactoryTests.cs" />
6363
<Compile Include="Factories\AutoFixtureFactoryTests.cs" />
64-
<Compile Include="Factories\ConstructorFactoryTests.cs" />
64+
<Compile Include="Factories\CallConstructorFactoryTests.cs" />
6565
<Compile Include="Factories\PublicPropertySettersFactoryTests.cs" />
6666
<Compile Include="Stubs\Entities\Company.cs" />
6767
<Compile Include="Stubs\Entities\Customer.cs" />

TestStack.Dossier/Factories/ConstructorFactory.cs renamed to TestStack.Dossier/Factories/CallConstructorFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
namespace TestStack.Dossier.Factories
55
{
66
/// <summary>
7-
/// Builds the object using the constructor with the most arguments.
7+
/// Builds the object using the constructor with the most arguments using values stored in the builder that match
8+
/// the constructor parameter name case insensitively.
9+
/// If there is no value specified in the builder for an argument then the builder will supply an anonymous value.
810
/// </summary>
9-
public class ConstructorFactory : IFactory
11+
public class CallConstructorFactory : IFactory
1012
{
1113
/// <inheritdoc />
1214
public virtual TObject BuildObject<TObject, TBuilder>(TestDataBuilder<TObject, TBuilder> builder)

TestStack.Dossier/Factories/PublicPropertySettersFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace TestStack.Dossier.Factories
55
/// <summary>
66
/// Creates an instance of an object by setting all public properties but not private properties.
77
/// </summary>
8-
public class PublicPropertySettersFactory : ConstructorFactory
8+
public class PublicPropertySettersFactory : CallConstructorFactory
99
{
1010
/// <inheritdoc />
1111
public override TObject BuildObject<TObject, TBuilder>(TestDataBuilder<TObject, TBuilder> builder)

TestStack.Dossier/TestStack.Dossier.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<Compile Include="DataSources\Generators\SequentialGenerator.cs" />
8282
<Compile Include="EquivalenceClasses\StringEquivalenceClasses.cs" />
8383
<Compile Include="Factories\AutoFixtureFactory.cs" />
84-
<Compile Include="Factories\ConstructorFactory.cs" />
84+
<Compile Include="Factories\CallConstructorFactory.cs" />
8585
<Compile Include="Factories\AllPropertiesFactory.cs" />
8686
<Compile Include="Factories\PublicPropertySettersFactory.cs" />
8787
<Compile Include="IAnonymousValueSupplier.cs" />

0 commit comments

Comments
 (0)