-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
205c61e
Added a static Builder<T> class to emulate NBuilder behaviour
mwhelan c57abec
renamed PropertyNameGetter to Reflector to show additional behaviour
mwhelan 48efca4
removed generic CanSupply from IAnonymousValueSupplier
mwhelan 7632277
Updated PR to apply Rob's suggestions.
mwhelan 6551bff
Fixed merge conflicts from PR#32
mwhelan 4f95399
Introduced Object Builders
mwhelan c0c8dbe
Renamed FactoryRegistry to ObjectBuilderRegistry
mwhelan a2602a6
Renamed object builders to strategies
mwhelan 8f0dc5f
Settled on object builder design
mwhelan 37a290d
Updated Builder to allow customisation of factory
mwhelan 72280f3
Added tests for various Factories
mwhelan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
.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
14
TestStack.Dossier.Tests/Builders/AutoConstructorCustomerBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
TestStack.Dossier.Tests/Builders/ProxyAlteringCustomerBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
TestStack.Dossier.Tests/Builders/StudentViewModelBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?