Skip to content

Commit 383070a

Browse files
committed
Added the ability to easily generate child objects using builders that share the anonymous value fixture
1 parent 7f7642f commit 383070a

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace NTestDataBuilder.Tests
6+
{
7+
public class ChildBuilderTests
8+
{
9+
[Fact]
10+
public void WhenNotUsingChildBuilder_ThenTheAnonymousValueFixtureIsNotReused()
11+
{
12+
var parent = new ParentBuilder().Build();
13+
14+
parent.ParentEnum.ShouldBe(parent.Child.ChildEnum);
15+
}
16+
17+
[Fact]
18+
public void WhenUsingChildBuilder_ThenTheAnonymousValueFixtureIsReused()
19+
{
20+
var parent = new ParentBuilder().WithChildBuilder().Build();
21+
22+
parent.ParentEnum.ShouldNotBe(parent.Child.ChildEnum);
23+
}
24+
25+
[Fact]
26+
public void WhenUsingChildBuilderIncludingModifier_ThenTheModifierGetsApplied()
27+
{
28+
const int number = 2;
29+
var parent = new ParentBuilder()
30+
.WithChildBuilder(b => b.WithANumber(number))
31+
.Build();
32+
33+
parent.Child.Number.ShouldBe(number);
34+
}
35+
}
36+
37+
public enum AnEnum
38+
{
39+
One,
40+
Two
41+
}
42+
43+
public class ParentObject
44+
{
45+
public ParentObject(AnEnum parentEnum, ChildObject child)
46+
{
47+
ParentEnum = parentEnum;
48+
Child = child;
49+
}
50+
51+
public AnEnum ParentEnum { get; private set; }
52+
public ChildObject Child { get; private set; }
53+
}
54+
55+
public class ChildObject
56+
{
57+
public ChildObject(AnEnum childEnum, int number)
58+
{
59+
ChildEnum = childEnum;
60+
Number = number;
61+
}
62+
63+
public AnEnum ChildEnum { get; private set; }
64+
public int Number { get; private set; }
65+
}
66+
67+
public class ParentBuilder : TestDataBuilder<ParentObject, ParentBuilder>
68+
{
69+
public ParentBuilder()
70+
{
71+
Set(x => x.Child, new ChildBuilder().Build());
72+
}
73+
74+
public ParentBuilder WithChildBuilder(Func<ChildBuilder, ChildBuilder> modifier = null)
75+
{
76+
return Set(x => x.Child, GetChildBuilder<ChildObject, ChildBuilder>(modifier).Build());
77+
}
78+
79+
protected override ParentObject BuildObject()
80+
{
81+
return new ParentObject(Get(x => x.ParentEnum), Get(x => x.Child));
82+
}
83+
}
84+
85+
public class ChildBuilder : TestDataBuilder<ChildObject, ChildBuilder>
86+
{
87+
public ChildBuilder()
88+
{
89+
WithANumber(1);
90+
}
91+
92+
public ChildBuilder WithANumber(int number)
93+
{
94+
return Set(x => x.Number, number);
95+
}
96+
97+
protected override ChildObject BuildObject()
98+
{
99+
return new ChildObject(Get(x => x.ChildEnum), Get(x => x.Number));
100+
}
101+
}
102+
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
</Reference>
5050
</ItemGroup>
5151
<ItemGroup>
52+
<Compile Include="ChildBuilderTests.cs" />
5253
<Compile Include="Entities\CustomerClass.cs" />
5354
<Compile Include="DataSources\DataSourceTests.cs" />
5455
<Compile Include="DataSources\Dictionaries\CacheTests.cs" />

NTestDataBuilder/TestDataBuilder.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,22 @@ public virtual bool IsListBuilderProxy()
142142
{
143143
return ListBuilder != null;
144144
}
145+
146+
/// <summary>
147+
/// Creates (and optionally modifies) a child builder class of this builder; sharing the anonymous value fixture.
148+
/// </summary>
149+
/// <typeparam name="TChildObject">The type of the child object being built</typeparam>
150+
/// <typeparam name="TChildBuilder">The type of the builder for the child object being built</typeparam>
151+
/// <param name="modifier">An optional modifier lambda expression with fluent builder method calls for the child builder</param>
152+
/// <returns>The instance of the child builder</returns>
153+
protected virtual TChildBuilder GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)
154+
where TChildObject : class
155+
where TChildBuilder : TestDataBuilder<TChildObject, TChildBuilder>, new()
156+
{
157+
var childBuilder = new TChildBuilder {Any = Any};
158+
if (modifier != null)
159+
modifier(childBuilder);
160+
return childBuilder;
161+
}
145162
}
146163
}

0 commit comments

Comments
 (0)