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 overriding the BuildObject is optional. It can be useful to do this 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 choose to override this method then Dossier will create the object for you.

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

Expand Down
24 changes: 3 additions & 21 deletions TestStack.Dossier.Tests/BuildListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using TestStack.Dossier.DataSources.Generators;
using TestStack.Dossier.Lists;
using TestStack.Dossier.Tests.Builders;
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Tests.Stubs.Entities;
using Xunit;

namespace TestStack.Dossier.Tests
Expand Down Expand Up @@ -70,33 +70,15 @@ public void GivenListOfBuilders_WhenCallingBuildListExplicitly_ThenAListOfUnique

var entities = builders.BuildList();

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]);
entities.ShouldBeUnique();
}

[Fact]
public void GivenListOfBuilders_WhenCallingBuildListImplicitly_ThenAListOfUniqueEntitiesShouldBeReturned()
{
List<Customer> entities = BasicCustomerBuilder.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]);
entities.ShouldBeUnique();
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion TestStack.Dossier.Tests/BuildTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Shouldly;
using TestStack.Dossier.Tests.Builders;
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Tests.Stubs.Entities;
using Xunit;

namespace TestStack.Dossier.Tests
Expand Down
171 changes: 171 additions & 0 deletions TestStack.Dossier.Tests/BuilderBuildListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
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.Stubs.ViewModels;
using Xunit;

namespace TestStack.Dossier.Tests
{
public class BuilderBuildListTests
{
private DateTime _enrollmentDate = new DateTime(2004, 9, 9);

[Fact]
public void GivenANormalBuilderInstance_WhenCallingIsListBuilderProxy_ThenReturnFalse()
{
var builder = Builder<StudentViewModel>.CreateNew();

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

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

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

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

var entities = builders.BuildList();

entities.Count.ShouldBe(5);
}

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

entities.Count.ShouldBe(5);
}

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

var entities = builders.BuildList();

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

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

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

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

var entities = builders.BuildList();

entities.ShouldBeUnique();
}

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

entities.ShouldBeUnique();
}

[Fact]
public void GivenListOfBuildersWithCustomisation_WhenBuildingEntitiesExplicitly_ThenTheCustomisationShouldTakeEffect()
{
var generator = new SequentialGenerator(0, 100);
var list = StudentViewModelBuilder.CreateListOfSize(3)
.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<StudentViewModel> data = StudentViewModelBuilder.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 studentViewModels = StudentViewModelBuilder.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().WhoEntrolledIn(_enrollmentDate)
.BuildList();

studentViewModels.ShouldBeAssignableTo<IList<StudentViewModel>>();
studentViewModels.Count.ShouldBe(5);
studentViewModels[0].FirstName.ShouldBe("First");
studentViewModels[1].LastName.ShouldBe("Next Last");
studentViewModels[2].LastName.ShouldBe("last1");
studentViewModels[3].LastName.ShouldBe("last2");
studentViewModels[4].LastName.ShouldBe("Last Last");
studentViewModels.ShouldAllBe(c => c.EnrollmentDate == _enrollmentDate);
}

[Fact]
public void GivenListOfBuildersWithARangeOfCustomisationMethods_WhenBuildingEntitiesImplicitly_ThenThenTheListIsBuiltAndModifiedCorrectly()
{
var i = 0;
List<StudentViewModel> studentViewModels = StudentViewModelBuilder.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().WhoEntrolledIn(_enrollmentDate);

studentViewModels.ShouldBeAssignableTo<IList<StudentViewModel>>();
studentViewModels.Count.ShouldBe(5);
studentViewModels[0].FirstName.ShouldBe("First");
studentViewModels[1].LastName.ShouldBe("Next Last");
studentViewModels[2].LastName.ShouldBe("last1");
studentViewModels[3].LastName.ShouldBe("last2");
studentViewModels[4].LastName.ShouldBe("Last Last");
studentViewModels.ShouldAllBe(c => c.EnrollmentDate == _enrollmentDate);
}

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

studentViewModels.Select(x => x.Grade).ShouldBeUnique();
}

[Fact]
public void WhenBuildingEntitiesImplicitly_ThenTheAnonymousValueFixtureIsSharedAcrossBuilders()
{
List<StudentViewModel> studentViewModels = StudentViewModelBuilder.CreateListOfSize(5);

studentViewModels.Select(x => x.Grade).ShouldBeUnique();
}
}
}
72 changes: 72 additions & 0 deletions TestStack.Dossier.Tests/BuilderBuildTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Shouldly;
using TestStack.Dossier.Tests.Builders;
using TestStack.Dossier.Tests.Stubs.Entities;
using TestStack.Dossier.Tests.Stubs.ViewModels;
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);
}

[Fact]
public void GivenBuilder_WhenBuildingObjectWithCtorAndPrivateSetters_ShouldSetPrivateSetters()
{
var id = Guid.NewGuid();
InstructorViewModel instructor = Builder<InstructorViewModel>.CreateNew()
.Set(x => x.FirstName, "Pi")
.Set(x => x.LastName, "Lanningham")
.Set(x => x.Id, id);

instructor.FirstName.ShouldBe("Pi");
instructor.LastName.ShouldBe("Lanningham");
instructor.Id.ShouldBe(id);
}
}
}
14 changes: 4 additions & 10 deletions TestStack.Dossier.Tests/Builders/AutoConstructorCustomerBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Factories;
using TestStack.Dossier.Tests.Stubs.Entities;

namespace TestStack.Dossier.Tests.Builders
{
class AutoConstructorCustomerBuilder : TestDataBuilder<Customer, AutoConstructorCustomerBuilder>
{
protected override Customer BuildObject()
{
return BuildByConstructor();
}
public AutoConstructorCustomerBuilder()
: base(new ConstructorFactory()) { }

public AutoConstructorCustomerBuilder WithFirstName(string firstName)
{
Expand Down
2 changes: 1 addition & 1 deletion TestStack.Dossier.Tests/Builders/BasicCustomerBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Linq.Expressions;
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Tests.Stubs.Entities;

namespace TestStack.Dossier.Tests.Builders
{
Expand Down
2 changes: 1 addition & 1 deletion TestStack.Dossier.Tests/Builders/CustomerBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Tests.Stubs.Entities;

namespace TestStack.Dossier.Tests.Builders
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using NSubstitute;
using TestStack.Dossier.Tests.Entities;
using TestStack.Dossier.Tests.Stubs.Entities;

namespace TestStack.Dossier.Tests.Builders
{
Expand Down
23 changes: 23 additions & 0 deletions TestStack.Dossier.Tests/Builders/StudentViewModelBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using TestStack.Dossier.Tests.Stubs.ViewModels;

namespace TestStack.Dossier.Tests.Builders
{
public class StudentViewModelBuilder : TestDataBuilder<StudentViewModel, StudentViewModelBuilder>
{
public virtual StudentViewModelBuilder WithFirstName(string firstName)
{
return Set(x => x.FirstName, firstName);
}

public virtual StudentViewModelBuilder WithLastName(string lastName)
{
return Set(x => x.LastName, lastName);
}

public virtual StudentViewModelBuilder WhoEntrolledIn(DateTime enrollmentDate)
{
return Set(x => x.EnrollmentDate, enrollmentDate);
}
}
}
Loading