Skip to content

Commit 49f43da

Browse files
committed
Ensured that building a list of entities uses the same anonymous value fixture
This means things like enums will alternate between the generated objects
1 parent 8959bd4 commit 49f43da

File tree

10 files changed

+36
-8
lines changed

10 files changed

+36
-8
lines changed

BREAKING_CHANGES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you want to fix a static value for a property then by all means you can still
1919
Version 2.0.0
2020
-------------
2121

22-
The way that lists are generated no longer uses NBuilder - the new syntax is backwards compatible with NBuilder except that the namespace you need to include is different. You can also refactor your list generation to be a lot more terse, but that is optional. Any `BuildList` extension methods you created will now need to be deleted since they are no longer needed.
22+
The way that lists are generated no longer uses NBuilder - the new syntax is backwards compatible with NBuilder except that the namespace you need to include is different. You can also refactor your list generation to be a lot more terse, but that is optional. Any `BuildList` extension methods you created will now need to be deleted since they are no longer needed. You also need to ensure that all of the methods you call are marked virtual so the list generation can proxy those method calls.
2323

2424
### Reason
2525
In order to support a new, much terser syntax for generating lists we rewrote the list generation code ourselves. You can now do this:
@@ -44,7 +44,7 @@ You also no longer need a custom extension method for the `BuildList` method so
4444

4545
### Fix
4646

47-
Simply add the following to the files that generate lists of builders and the existing syntax should work:
47+
Simply add the following to the files that generate lists of builders and change your builder modification methods to be virtual and the existing syntax should work:
4848

4949
```
5050
using NTestDataBuilder.Lists;

NTestDataBuilder.Tests/BuildListTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,17 @@ public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEnti
8484
customers[4].LastName.ShouldBe("Last Last");
8585
customers.ShouldAllBe(c => c.YearJoined == 1999);
8686
}
87+
88+
[Fact]
89+
public void WhenBuildingEntities_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders()
90+
{
91+
var customers = CustomerBuilder.CreateListOfSize(5).BuildList();
92+
93+
customers[0].CustomerClass.ShouldBe(CustomerClass.Normal);
94+
customers[1].CustomerClass.ShouldBe(CustomerClass.Bronze);
95+
customers[2].CustomerClass.ShouldBe(CustomerClass.Silver);
96+
customers[3].CustomerClass.ShouldBe(CustomerClass.Gold);
97+
customers[4].CustomerClass.ShouldBe(CustomerClass.Platinum);
98+
}
8799
}
88100
}

NTestDataBuilder.Tests/Builders/BasicCustomerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class BasicCustomerBuilder : TestDataBuilder<Customer, BasicCustomerBuild
88
{
99
protected override Customer BuildObject()
1010
{
11-
return new Customer("First Name", "Last Name", 2013);
11+
return new Customer("First Name", "Last Name", 2013, CustomerClass.Normal);
1212
}
1313

1414
public new BasicCustomerBuilder Set<TValue>(Expression<Func<Customer, TValue>> property, TValue value)

NTestDataBuilder.Tests/Builders/CustomerBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ protected override Customer BuildObject()
2424
return new Customer(
2525
Get(x => x.FirstName),
2626
Get(x => x.LastName),
27-
Get(x => x.YearJoined)
27+
Get(x => x.YearJoined),
28+
Get(x => x.CustomerClass)
2829
);
2930
}
3031
}

NTestDataBuilder.Tests/Entities/Customer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Customer
66
{
77
protected Customer() {}
88

9-
public Customer(string firstName, string lastName, int yearJoined)
9+
public Customer(string firstName, string lastName, int yearJoined, CustomerClass customerClass)
1010
{
1111
if (string.IsNullOrEmpty(firstName))
1212
throw new ArgumentNullException("firstName");
@@ -16,6 +16,7 @@ public Customer(string firstName, string lastName, int yearJoined)
1616
FirstName = firstName;
1717
LastName = lastName;
1818
YearJoined = yearJoined;
19+
CustomerClass = customerClass;
1920
}
2021

2122
public virtual int CustomerForHowManyYears(DateTime since)
@@ -28,5 +29,6 @@ public virtual int CustomerForHowManyYears(DateTime since)
2829
public virtual string FirstName { get; private set; }
2930
public virtual string LastName { get; private set; }
3031
public virtual int YearJoined { get; private set; }
32+
public virtual CustomerClass CustomerClass { get; private set; }
3133
}
3234
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NTestDataBuilder.Tests.Entities
2+
{
3+
public enum CustomerClass
4+
{
5+
Normal,
6+
Bronze,
7+
Silver,
8+
Gold,
9+
Platinum
10+
}
11+
}

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="Entities\CustomerClass.cs" />
5253
<Compile Include="DataSources\DataSourceTests.cs" />
5354
<Compile Include="DataSources\Dictionaries\CacheTests.cs" />
5455
<Compile Include="DataSources\Dictionaries\FileDictionaryRepositoryIntegrationTests.cs" />

NTestDataBuilder/Lists/EnsureAllMethodsVirtual.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void MethodsInspected()
1313

1414
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
1515
{
16-
if (new[]{"get_Any", "Build", "AsProxy", "Get", "GetOrDefault", "Set", "Has", "get_ListBuilder", "set_ListBuilder"}.Contains(memberInfo.Name))
16+
if (new[]{"get_Any", "set_Any", "Build", "AsProxy", "Get", "GetOrDefault", "Set", "Has", "get_ListBuilder", "set_ListBuilder"}.Contains(memberInfo.Name))
1717
return;
1818
throw new InvalidOperationException(string.Format("Tried to build a list with a builder who has non-virtual method. Please make {0} on type {1} virtual.", memberInfo.Name, type.Name));
1919
}

NTestDataBuilder/Lists/ListBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ internal ListBuilder(int size)
2424
.CreateClassProxy(typeof (TBuilder), new ProxyGenerationOptions(new EnsureAllMethodsVirtual()), new ListBuilderInterceptor<TObject, TBuilder>(this));
2525
BuilderProxy.ListBuilder = this;
2626
_list = new List<TBuilder>();
27+
var fixture = new AnonymousValueFixture();
2728
for (var i = 0; i < size; i++)
28-
_list.Add(new TBuilder());
29+
_list.Add(new TBuilder {Any = fixture});
2930
}
3031

3132
internal TBuilder BuilderProxy { get; private set; }

NTestDataBuilder/TestDataBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected TestDataBuilder()
2929
/// <summary>
3030
/// Generate anonymous data using this fixture - one instance per builder instance.
3131
/// </summary>
32-
public AnonymousValueFixture Any { get; private set; }
32+
public AnonymousValueFixture Any { get; internal set; }
3333

3434
/// <summary>
3535
/// Build the object.

0 commit comments

Comments
 (0)