Skip to content

Commit 1da1625

Browse files
committed
Setups can now be created without a filler and you can encapsulate setups with other setups.
1 parent 706ef91 commit 1da1625

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

ObjectFiller.Test/SetupTests.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class Parent
1515

1616
public class Child
1717
{
18-
public int Value { get; set; }
18+
public int IntValue { get; set; }
19+
20+
public string StringValue { get; set; }
1921
}
2022

2123
[TestMethod]
@@ -24,10 +26,10 @@ public void RandomizerCreatesObjectsBasedOnPreviouseSetups()
2426
int givenValue = Randomizer<int>.Create();
2527

2628
var childFiller = new Filler<Child>();
27-
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
29+
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;
2830

2931
var child = Randomizer<Child>.Create(childSetup);
30-
Assert.AreEqual(givenValue, child.Value);
32+
Assert.AreEqual(givenValue, child.IntValue);
3133
}
3234

3335
[TestMethod]
@@ -36,13 +38,13 @@ public void UseSetupsAgainForPropertyConfigurations()
3638
int givenValue = Randomizer<int>.Create();
3739

3840
var childFiller = new Filler<Child>();
39-
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
41+
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;
4042

4143
var parentFiller = new Filler<Parent>();
4244
parentFiller.Setup().OnProperty(x => x.Child).Use(childSetup);
4345

4446
var parent = parentFiller.Create();
45-
Assert.AreEqual(givenValue, parent.Child.Value);
47+
Assert.AreEqual(givenValue, parent.Child.IntValue);
4648
}
4749

4850
[TestMethod]
@@ -51,13 +53,13 @@ public void UseSetupsAgainForTypeConfigurations()
5153
int givenValue = Randomizer<int>.Create();
5254

5355
var childFiller = new Filler<Child>();
54-
var childSetup = childFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
56+
var childSetup = childFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;
5557

5658
var parentFiller = new Filler<Parent>();
5759
parentFiller.Setup().OnType<Child>().Use(childSetup);
5860

5961
var parent = parentFiller.Create();
60-
Assert.AreEqual(givenValue, parent.Child.Value);
62+
Assert.AreEqual(givenValue, parent.Child.IntValue);
6163
}
6264

6365
[TestMethod]
@@ -66,16 +68,34 @@ public void UseSetupsAgain()
6668
int givenValue = Randomizer<int>.Create();
6769

6870
var firstChildFiller = new Filler<Child>();
69-
var childSetup = firstChildFiller.Setup().OnProperty(x => x.Value).Use(givenValue).Result;
71+
var childSetup = firstChildFiller.Setup().OnProperty(x => x.IntValue).Use(givenValue).Result;
7072

7173
var secondChildFiller = new Filler<Child>();
7274
secondChildFiller.Setup(childSetup);
7375

7476
var child = secondChildFiller.Create();
7577

76-
Assert.AreEqual(givenValue, child.Value);
78+
Assert.AreEqual(givenValue, child.IntValue);
7779
}
7880

81+
[TestMethod]
82+
public void SetupsCanBeCreatedWithFactoryMethod()
83+
{
84+
var childSetup = FillerSetup.Create<Child>().OnProperty(x => x.IntValue).Use(42).Result;
85+
86+
var child = Randomizer<Child>.Create(childSetup);
87+
Assert.AreEqual(42, child.IntValue);
88+
}
7989

90+
[TestMethod]
91+
public void SetupsCanBeCreatedWithFactoryMethodBasedOnExistingSetupManager()
92+
{
93+
var childSetup = FillerSetup.Create<Child>().OnProperty(x => x.IntValue).Use(42).Result;
94+
childSetup = FillerSetup.Create<Child>(childSetup).OnProperty(x => x.StringValue).Use("Juchu").Result;
95+
96+
var child = Randomizer<Child>.Create(childSetup);
97+
Assert.AreEqual(42, child.IntValue);
98+
Assert.AreEqual("Juchu", child.StringValue);
99+
}
80100
}
81101
}

ObjectFiller/Setup/FillerSetup.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,29 @@ public FillerSetup()
3535
/// Gets the <see cref="FillerSetupItem"/> for a specific <see cref="Type"/>
3636
/// </summary>
3737
internal Dictionary<Type, FillerSetupItem> TypeToFillerSetup { get; private set; }
38+
39+
/// <summary>
40+
/// Creates a setup for a specific type.
41+
/// </summary>
42+
/// <typeparam name="TTarget">The type which is configured.</typeparam>
43+
/// <returns>Setup you can use for Filler and Randomizer.</returns>
44+
public static FluentFillerApi<TTarget> Create<TTarget>() where TTarget : class
45+
{
46+
return new FluentFillerApi<TTarget>(new SetupManager());
47+
}
48+
49+
/// <summary>
50+
/// Creates a setup based uppon another setup
51+
/// </summary>
52+
/// <typeparam name="TTarget">The type which is configured.</typeparam>
53+
/// <param name="baseSetup">The setup which is used as basis of the new one.</param>
54+
/// <returns>Setup you can use for Filler and Randomizer.</returns>
55+
public static FluentFillerApi<TTarget> Create<TTarget>(FillerSetup baseSetup) where TTarget : class
56+
{
57+
var setupManager = new SetupManager();
58+
setupManager.FillerSetup = baseSetup ?? new FillerSetup();
59+
60+
return new FluentFillerApi<TTarget>(setupManager);
61+
}
3862
}
3963
}

0 commit comments

Comments
 (0)