Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a static Builder<T> class to emulate NBuilder behaviour #31

Merged
merged 11 commits into from
May 15, 2015
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Prior to v2.0 this library was known as NTestDataBuilder.
}
}

Note that you can optionally override the `BuildObject` method if you want full control over how your object is constructed, for example if you want to use a particular constructor. If you don't provide this then Dossier will create the object for you.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is a bit confusing given the example preceeding it has the BuildObject method overridden.

Perhaps something like: "Overriding the BuildObject method is optional - if you don't then blagh. It makes sense to override it when blah."


3. Use the builder in a test, e.g.

Expand Down
194 changes: 194 additions & 0 deletions TestStack.Dossier.Tests/BuilderBuildListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using TestStack.Dossier.DataSources.Generators;
using TestStack.Dossier.Lists;
using TestStack.Dossier.Tests.Builders;
using TestStack.Dossier.Tests.Entities;
using Xunit;

namespace TestStack.Dossier.Tests
{
public class BuilderBuildListTests
{
[Fact]
public void GivenANormalBuilderInstance_WhenCallingIsListBuilderProxy_ThenReturnFalse()
{
var builder = Builder<Customer>.CreateNew();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create a separate DTO class for these tests so the test indicate the usage that we intend for the generic builder (non-domain entities).


builder.IsListBuilderProxy().ShouldBe(false);
}

[Fact]
public void GivenAListBuilderProxyInstance_WhenCallingIsListBuilderProxy_ThenReturnTrue()
{
var builder = Builder<Customer>.CreateListOfSize(1).TheFirst(1);

builder.IsListBuilderProxy().ShouldBe(true);
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfEntitiesOfTheRightSizeShouldBeReturned()
{
var builders = Builder<Customer>.CreateListOfSize(5);

var entities = builders.BuildList();

entities.Count.ShouldBe(5);
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfEntitiesOfTheRightSizeShouldBeReturned()
{
List<Customer> entities = Builder<Customer>.CreateListOfSize(5);

entities.Count.ShouldBe(5);
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfEntitiesOfTheRightTypeShouldBeReturned()
{
var builders = Builder<Customer>.CreateListOfSize(5);

var entities = builders.BuildList();

entities.ShouldBeAssignableTo<IList<Customer>>();
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfEntitiesOfTheRightTypeShouldBeReturned()
{
List<Customer> entities = Builder<Customer>.CreateListOfSize(5);

entities.ShouldBeAssignableTo<IList<Customer>>();
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfUniqueEntitiesShouldBeReturned()
{
var builders = Builder<Customer>.CreateListOfSize(5);

var entities = builders.BuildList();

entities[0].ShouldNotBe(entities[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Shouldly have a ShouldAllBeUnique() or similar? can't remember.

entities[0].ShouldNotBe(entities[2]);
entities[0].ShouldNotBe(entities[3]);
entities[0].ShouldNotBe(entities[4]);
entities[1].ShouldNotBe(entities[2]);
entities[1].ShouldNotBe(entities[3]);
entities[1].ShouldNotBe(entities[4]);
entities[2].ShouldNotBe(entities[3]);
entities[2].ShouldNotBe(entities[4]);
entities[3].ShouldNotBe(entities[4]);
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfUniqueEntitiesShouldBeReturned()
{
List<Customer> entities = Builder<Customer>.CreateListOfSize(5);

entities[0].ShouldNotBe(entities[1]);
entities[0].ShouldNotBe(entities[2]);
entities[0].ShouldNotBe(entities[3]);
entities[0].ShouldNotBe(entities[4]);
entities[1].ShouldNotBe(entities[2]);
entities[1].ShouldNotBe(entities[3]);
entities[1].ShouldNotBe(entities[4]);
entities[2].ShouldNotBe(entities[3]);
entities[2].ShouldNotBe(entities[4]);
entities[3].ShouldNotBe(entities[4]);
}

[Fact]
public void GivenListOfBuildersWithCustomisation_WhenBuildingEntitiesExplicitly_ThenTheCustomisationShouldTakeEffect()
{
var generator = new SequentialGenerator(0, 100);
var list = CustomerBuilder.CreateListOfSize(3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused abotu these list tests - don't we already have tests testing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were a copy of the BuilderListTests, using Builder in place of CustomerBuilder. As part of the design process I wanted to ensure that the new Builder would have the same functionality as a CustomerBuilder by having it satisfy the same specification.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake. That's the BuilderBuildListTests.cs file. I made duplications here when I added implicit operator. So I differentiate the name with Explicitly (calling Build) or Implicitly. Again, I wanted to ensure the same behaviour.

.All().With(b => b.WithFirstName(generator.Generate().ToString()));

var data = list.BuildList();

data.Select(c => c.FirstName).ToArray()
.ShouldBe(new[] { "0", "1", "2" });
}

[Fact]
public void GivenListOfBuildersWithCustomisation_WhenBuildingEntitiesImplicitly_ThenTheCustomisationShouldTakeEffect()
{
var generator = new SequentialGenerator(0, 100);

List<Customer> data = CustomerBuilder.CreateListOfSize(3)
.All().With(b => b.WithFirstName(generator.Generate().ToString()));

data.Select(c => c.FirstName).ToArray()
.ShouldBe(new[] { "0", "1", "2" });
}

[Fact]
public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEntitiesExplicitly_ThenThenTheListIsBuiltAndModifiedCorrectly()
{
var i = 0;
var customers = CustomerBuilder.CreateListOfSize(5)
.TheFirst(1).WithFirstName("First")
.TheNext(1).WithLastName("Next Last")
.TheLast(1).WithLastName("Last Last")
.ThePrevious(2).With(b => b.WithLastName("last" + (++i).ToString()))
.All().WhoJoinedIn(1999)
.BuildList();

customers.ShouldBeAssignableTo<IList<Customer>>();
customers.Count.ShouldBe(5);
customers[0].FirstName.ShouldBe("First");
customers[1].LastName.ShouldBe("Next Last");
customers[2].LastName.ShouldBe("last1");
customers[3].LastName.ShouldBe("last2");
customers[4].LastName.ShouldBe("Last Last");
customers.ShouldAllBe(c => c.YearJoined == 1999);
}

[Fact]
public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEntitiesImplicitly_ThenThenTheListIsBuiltAndModifiedCorrectly()
{
var i = 0;
List<Customer> customers = CustomerBuilder.CreateListOfSize(5)
.TheFirst(1).WithFirstName("First")
.TheNext(1).WithLastName("Next Last")
.TheLast(1).WithLastName("Last Last")
.ThePrevious(2).With(b => b.WithLastName("last" + (++i).ToString()))
.All().WhoJoinedIn(1999);

customers.ShouldBeAssignableTo<IList<Customer>>();
customers.Count.ShouldBe(5);
customers[0].FirstName.ShouldBe("First");
customers[1].LastName.ShouldBe("Next Last");
customers[2].LastName.ShouldBe("last1");
customers[3].LastName.ShouldBe("last2");
customers[4].LastName.ShouldBe("Last Last");
customers.ShouldAllBe(c => c.YearJoined == 1999);
}

[Fact]
public void WhenBuildingEntitiesExplicitly_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders()
{
var customers = CustomerBuilder.CreateListOfSize(5).BuildList();

customers[0].CustomerClass.ShouldBe(CustomerClass.Normal);
customers[1].CustomerClass.ShouldBe(CustomerClass.Bronze);
customers[2].CustomerClass.ShouldBe(CustomerClass.Silver);
customers[3].CustomerClass.ShouldBe(CustomerClass.Gold);
customers[4].CustomerClass.ShouldBe(CustomerClass.Platinum);
}

[Fact]
public void WhenBuildingEntitiesImplicitly_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders()
{
List<Customer> customers = CustomerBuilder.CreateListOfSize(5);

customers[0].CustomerClass.ShouldBe(CustomerClass.Normal);
customers[1].CustomerClass.ShouldBe(CustomerClass.Bronze);
customers[2].CustomerClass.ShouldBe(CustomerClass.Silver);
customers[3].CustomerClass.ShouldBe(CustomerClass.Gold);
customers[4].CustomerClass.ShouldBe(CustomerClass.Platinum);
}
}
}
56 changes: 56 additions & 0 deletions TestStack.Dossier.Tests/BuilderBuildTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Shouldly;
using TestStack.Dossier.Tests.Builders;
using TestStack.Dossier.Tests.Entities;
using Xunit;

namespace TestStack.Dossier.Tests
{
public class BuilderBuildTests
{
[Fact]
public void GivenBasicBuilder_WhenCallingBuildExplicitly_ThenReturnAnObject()
{
var builder = Builder<Customer>.CreateNew();

var customer = builder.Build();

customer.ShouldBeOfType<Customer>();
}

[Fact]
public void GivenBuilder_WhenCallingSetExplicitly_ShouldOverrideValues()
{
var builder = Builder<Customer>.CreateNew()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that Customer doesn't have public setters on it's properties I don't understand how this test could pass?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to agree with you it's surprising! I've had to add a check for property.CanWrite now (because it was trying to write to a property with just a getter) and it returns true for all of the properties with private set and property.SetValue is able to write to the property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a way to detect private setters? I don't want to set values on private setters, it removes the whole point of being able to build DDD entities that are valid...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if that is a concern, then the consumer will override the BuildObject method and construct it with the appropriate constructor, using the original behaviour? This is potentially a benefit for view models and DTOs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we have a default implementation of BuildObject it will get used by people for DDD objects because it won't be clear that 's not a good idea (it wouldn't necessarily be obvious what was happening).

I wonder if for now we put this BuildObject definition in Builder<T> for now to avoid these issues for this PR then we can look at how we support a default implementation of BuildObject as part of addressing #24?

It's still slightly worrying to me that it sets private setter values though - I'm not sure if that would be expected behaviour. I wouldn't object if there was a config option to turn that on (but I think it should be opt in rather than opt out).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would find it frustrating to have to implement BuildObject for every builder that implements TestDataBuilder. Of course, I can create a base builder in my test infrastructure that implements it, so it's not a show stopper. But as someone coming from NBuilder, which didn't require that, I would find it unintuitive about why this library requires that I need to do that. Surely the whole point of a virtual method is that you have the best of both worlds - you get the default behaviour or choose to override if you want something else. I'm concerned that making it abstract, and forcing people to override it, might alienate users as you are removing the choice?

.Set(x => x.FirstName, "Pi")
.Set(x => x.LastName, "Lanningham")
.Set(x => x.YearJoined, 2014);

var customer = builder.Build();

customer.FirstName.ShouldBe("Pi");
customer.LastName.ShouldBe("Lanningham");
customer.YearJoined.ShouldBe(2014);
}

[Fact]
public void GivenBasicBuilder_WhenCallingBuildImplicitly_ThenReturnAnObject()
{
Customer customer = Builder<Customer>.CreateNew();

customer.ShouldBeOfType<Customer>();
}

[Fact]
public void GivenBuilder_WhenCallingSetImplicitly_ShouldOverrideValues()
{
Customer customer = Builder<Customer>.CreateNew()
.Set(x => x.FirstName, "Pi")
.Set(x => x.LastName, "Lanningham")
.Set(x => x.YearJoined, 2014);

customer.FirstName.ShouldBe("Pi");
customer.LastName.ShouldBe("Lanningham");
customer.YearJoined.ShouldBe(2014);
}
}
}
14 changes: 13 additions & 1 deletion TestStack.Dossier.Tests/GetAnonymousTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Shouldly;
using System;
using Shouldly;
using TestStack.Dossier.DataSources.Person;
using TestStack.Dossier.Lists;
using TestStack.Dossier.Tests.Builders;
Expand Down Expand Up @@ -113,9 +114,20 @@ public bool CanSupplyValue<TObject, TValue>(string propertyName)
&& propertyName.ToLower().StartsWith("year");
}

public bool CanSupplyValue(Type type, string propertyName)
{
return type == typeof(int)
&& propertyName.ToLower().StartsWith("year");
}

public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName)
{
return (TValue)(object)_year++;
}

public object GenerateAnonymousValue(AnonymousValueFixture any, Type type, string propertyName)
{
return _year++;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TestStack.Dossier.Tests.TestHelpers
using System;

namespace TestStack.Dossier.Tests.TestHelpers
{
public class StaticAnonymousValueSupplier : IAnonymousValueSupplier
{
Expand All @@ -14,9 +16,19 @@ public bool CanSupplyValue<TObject, TValue>(string propertyName)
return typeof(TValue) == _valueToSupply.GetType();
}

public bool CanSupplyValue(Type type, string propertyName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the generic version anymore? If not can we delete them so there isn't so much duplicate logic?

That would be a braking change in the public API and require a v3 release (I'm comfortable with that though)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done all the changes apart from this. I agree it makes sense to change this, but it might be an idea to hold off on the breaking change and see if there might be more features we want to add for v3...?

{
return type == _valueToSupply.GetType();
}

public TValue GenerateAnonymousValue<TObject, TValue>(AnonymousValueFixture any, string propertyName)
{
return (TValue) _valueToSupply;
}

public object GenerateAnonymousValue(AnonymousValueFixture any, Type type, string propertyName)
{
return _valueToSupply;
}
}
}
2 changes: 2 additions & 0 deletions TestStack.Dossier.Tests/TestStack.Dossier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BuilderBuildListTests.cs" />
<Compile Include="BuilderBuildTests.cs" />
<Compile Include="ChildBuilderTests.cs" />
<Compile Include="Entities\CustomerClass.cs" />
<Compile Include="DataSources\DataSourceTests.cs" />
Expand Down
14 changes: 12 additions & 2 deletions TestStack.Dossier/AnonymousValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,23 @@ public AnonymousValueFixture()
/// <returns>The anonymous value, taking into account any registered conventions</returns>
public T Get<TObject, T>(Expression<Func<TObject, T>> property)
{
var propertyName = PropertyNameGetter.Get(property);
var propertyName = Reflector.GetPropertyFor(property);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this method be called GetPropertyNameFor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

var valueSupplier = LocalValueSuppliers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this call the non-generic Get and then cast the result?

.Concat(GlobalValueSuppliers)
.Concat(DefaultValueSuppliers)
.First(s => s.CanSupplyValue<TObject, T>(propertyName));
.First(s => s.CanSupplyValue(typeof(T), propertyName));

return valueSupplier.GenerateAnonymousValue<TObject, T>(this, propertyName);
}

public object Get(Type type, string propertyName)
{
var valueSupplier = LocalValueSuppliers
.Concat(GlobalValueSuppliers)
.Concat(DefaultValueSuppliers)
.First(s => s.CanSupplyValue(type,propertyName));

return valueSupplier.GenerateAnonymousValue(this, type, propertyName);
}
}
}
13 changes: 13 additions & 0 deletions TestStack.Dossier/Builder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace TestStack.Dossier
{
public class Builder<T> : TestDataBuilder<T, Builder<T>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta love how simple this is

where T : class
{
public static Builder<T> CreateNew()
{
return new Builder<T>();
}
}
}
Loading