A builder separates the concern of creation of an object into an other object.

You can pass a builder around which knows how to make a particular type of object conforming to an abstract or a concrete with particular data set .. or both

Its a great tool for testing when you are grouping a number of tests together which need a similar object and/or one with similar data.

A new data-rich app needs data to test against, and this is not necessarily "real" data, just quantities of the stuff, and for this you can use a number of tools which will populate collections of data with random values.

autopoco - ... building readable test data from Plain Old CLR Objects

nbuilder - Rapid Generation of Test objects for .NET

Both of these libraries allow you to create concrete objects, and get a nice set of testable data which you can work with, but neither allow you to create abstracts using mocks.

Creating objects from an abstract

We are all very familiar with using mocking libraries, where we can do something like ...

var obj = Mock.Of<IObj>()

So all I wanted to do was create masses of objects from an abstact using mocks.

I am sure I have only just scratched the surface, but so far it turned out to be really simple.

The basic idea was to have the builder object hold how to create a subject and then how to assign its values (fluently of course).

Then Build the object, but allow an optional assign override...

public class Builder<T>
{
    public Builder<T> Create(
        Func<T> create = null) ...

    public Builder<T> With(
        Action<T> assign) ...

    public T Build(
        Action<T> assign = null) ....
}

So for example, to create an object if type IThingy

var thingyBuilder = new Builder<IThingy>().Create(Mock.Of<IThingy>);

Now you have a builder ready to create an IThingy at your whim, and you can set the builder to assign values and/or invoke methods using

thingyBuilder.With(
    x=>{
            x.Property = "Yay";
            x.FunkyFi();
       });

And you can pass in an override on build too..

var item = thingyBuilder.Build(
    x => {
            x.Property = "Woo";
         });

The next step was to allow this to happen a number of times

public class Builder<T>
{
    ...

    public Builder<T> Build(
        int count,
        Action<T, int> assign = null)

    ...
}

Here you can pass in a count of the number of objects you want to create (and an optional assignment action), it returns a new builder so you can call the same again. When you are done with your genisis you simply access the items collection on the resultant builder and there you go.

var items = _builder
    .Build(expectedCount, (x, i) => x.Name = i + " barry")
    .Build(expectedCount, (x, i) => x.Name = i + " bob")
    .Items.ToArray();

The count is contigous in the fluent context and is passed to the assignment, if you have one.

For the code and tests showing examples of how to use it head over to github

http://mrantix.github.com/Testing/

Please let me know what you think

Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

yesterday
scottgu
I'm excited to announce the GA Release of AMQP support with the Windows Azure Service Bus. I blogged details: http://t.co/L6SzbFbkfh

yesterday
clemensv
Announcing the General Availability of AMQP 1.0 in Windows Azure Service Bus! http://t.co/2ZRFUoa5rO #Subscribe #ServiceBus #AMQP #Azure

yesterday
WindowsAzure
RT to share the news: Announcing the release of AMQP support with #WindowsAzure Service Bus. http://t.co/2QQhagZiSW

just now
pinkalchemy
Weekend Reading: May 24th Edition – The world meets Xbox One, and Windows Azure heads to Asia: In this edition... http://t.co/RgzCodOGMf

just now
NoNamesFree
PhoneGap is a nice idea but not ready in my eyes, i'm back to native app development.