A small sample on how to create data objects in a fluent(ish) way for unit testing.
Here is my blog post to explain a bit more.
public class AddressBuilder : BuilderBase<Address, AddressBuilder>
{
public AddressBuilder WithDefaultTestValues()
{
_concreteObject = new Address()
{
Suburb = "Suburb",
PostCode = "1234",
StreetNumber = 88
};
return this;
}
public AddressBuilder WithAustralianAddress()
{
_concreteObject = new Address()
{
Suburb = "Perth",
PostCode = "6000",
StreetNumber = 22
};
return this;
}
public AddressBuilder WithSouthAfricanAddress()
{
_concreteObject = new Address()
{
Suburb = "Sandton",
PostCode = "2066",
StreetNumber = 11
};
return this;
}
}
var builder = new EmployeeBuilder();
var actual = builder.WithEmployeeFromAustralia()
// mix things up a bit and add another address manually by instantiating a new AddressBuilder
.With(e => e.Addresses.Add(new AddressBuilder()
.WithSouthAfricanAddress()
.Build())
)
.Build();
var builder = new EmployeeBuilder();
var actual = builder.WithEmployeeFromAustralia()
.With<AddressBuilder>((e, addressBuilder) => e.Addresses.Add(addressBuilder
.WithSouthAfricanAddress()
.Build())
)
.Build();
var builder = new EmployeeBuilder();
var actual = builder.WithEmployeeFromAustralia()
.AddSouthAfricanAddress()
.Build();