Skip to content

Commit fc2689d

Browse files
committed
Added test for using global value supplier when generating list and updated the test CustomerBuilder with some of the new changes
1 parent 8ff41fb commit fc2689d

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

NTestDataBuilder.Tests/Builders/CustomerBuilder.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,19 @@ public static IList<Customer> BuildList(this IListBuilder<CustomerBuilder> list)
1919

2020
class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder>
2121
{
22-
public CustomerBuilder()
23-
{
24-
WithFirstName("Rob");
25-
WithLastName("Moore");
26-
WhoJoinedIn(2013);
27-
}
28-
2922
public CustomerBuilder WithFirstName(string firstName)
3023
{
31-
Set(x => x.FirstName, firstName);
32-
return this;
24+
return Set(x => x.FirstName, firstName);
3325
}
3426

3527
public CustomerBuilder WithLastName(string lastName)
3628
{
37-
Set(x => x.LastName, lastName);
38-
return this;
29+
return Set(x => x.LastName, lastName);
3930
}
4031

4132
public CustomerBuilder WhoJoinedIn(int yearJoined)
4233
{
43-
Set(x => x.YearJoined, yearJoined);
44-
return this;
34+
return Set(x => x.YearJoined, yearJoined);
4535
}
4636

4737
protected override Customer BuildObject()

NTestDataBuilder.Tests/GetAnonymousTests.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using NTestDataBuilder.Tests.Builders;
1+
using System.Linq;
2+
using FizzWare.NBuilder;
3+
using NTestDataBuilder.Tests.Builders;
4+
using NTestDataBuilder.Tests.Entities;
25
using NTestDataBuilder.Tests.TestHelpers;
36
using NUnit.Framework;
47

@@ -61,5 +64,41 @@ public void GivenNoValueHasBeenSetForAnIntProperty_WhenRetrievingTheValueForThat
6164

6265
Assert.That(val1, Is.Not.EqualTo(val2));
6366
}
67+
68+
[Test]
69+
public void GivenGlobalValueSupplierSet_WhenGeneratingList_UseTheSupplierForTheRelevantPropertyExceptWhereItsOverridden()
70+
{
71+
AnonymousValueFixture.GlobalValueSuppliers.Add(new YearValueSupplier());
72+
var customers = CustomerBuilder.CreateListOfSize(5)
73+
.TheLast(1).With(b => b.WhoJoinedIn(1990))
74+
.BuildList<Customer, CustomerBuilder>();
75+
76+
Assert.That(customers[0].YearJoined, Is.EqualTo(2000));
77+
Assert.That(customers[1].YearJoined, Is.EqualTo(2001));
78+
Assert.That(customers[2].YearJoined, Is.EqualTo(2002));
79+
Assert.That(customers[3].YearJoined, Is.EqualTo(2003));
80+
Assert.That(customers[4].YearJoined, Is.EqualTo(1990));
81+
}
82+
}
83+
84+
internal class YearValueSupplier : IAnonymousValueSupplier
85+
{
86+
private int _year;
87+
88+
public YearValueSupplier()
89+
{
90+
_year = 2000;
91+
}
92+
93+
public bool CanSupplyValue<TObject, TValue>(string propertyName)
94+
{
95+
return typeof(TValue) == typeof(int)
96+
&& propertyName.ToLower().StartsWith("year");
97+
}
98+
99+
public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName)
100+
{
101+
return (TValue)(object)_year++;
102+
}
64103
}
65104
}

0 commit comments

Comments
 (0)