Skip to content
A small library to create data objects in a fluent way for unit testing.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
BuilderLibrary
BuilderLibraryTests
Data.Models
.gitattributes
.gitignore
BuilderPattern.sln
LICENSE
README.md

README.md

Data Driven Builder Pattern Library

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.

Sample Builder Class

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;
    }
}

Samples in Tests

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();

You can’t perform that action at this time.