-
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
Changes from 3 commits
205c61e
c57abec
48efca4
7632277
6551bff
4f95399
c0c8dbe
a2602a6
8f0dc5f
37a290d
72280f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 commentThe 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 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 commentThe 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); | ||
} | ||
} | ||
} |
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 | ||
{ | ||
|
@@ -14,9 +16,19 @@ public bool CanSupplyValue<TObject, TValue>(string propertyName) | |
return typeof(TValue) == _valueToSupply.GetType(); | ||
} | ||
|
||
public bool CanSupplyValue(Type type, string propertyName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this method be called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
var valueSupplier = LocalValueSuppliers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>(); | ||
} | ||
} | ||
} |
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.
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."