Friday, February 08, 2008

Today I was creating a test data builder for a unit test. I am fairly new to test data builders, on the last project I was on I relied on the ObjectMother pattern which became fairly tedious over time. Both the ObjectMother pattern and the Test Data Builder are Test Helper patterns:

"We define a helper class to hold any Test Utility Methods we want to reuse in several tests." 

If your objects aren't immutable and all you really care about is getting sensible test data without having to repeat yourself, you can use the Rhino Mocks GenerateStub feature to provide sensible test data and deal with invariant test data:

 

[TestFixture]
public class InvoiceTests
{
    [Test]
    public void ShouldCreateInvoiceWithNoPostalCode()
    {
        Invoice invoiceWithNoPostalCode =
                new InvoiceBuilder()
                .WithRecipient(new RecipientBuilder()
                    .WithAddress(new AddressBuilder()
                        .WithNoPostalCode()
                        .Build())
                    .Build())
                .Build();
 
        Assert.AreEqual(string.Empty,
            invoiceWithNoPostalCode.Recipient.Address.PostalCode);
    }
 
 
}
 
public class InvoiceBuilder
{
    private Invoice invoice;
 
    public InvoiceBuilder()
    {
        invoice = MockRepository.GenerateStub<Invoice>();
    }
 
    public InvoiceBuilder WithRecipient(Recipient recipient)
    {
        invoice.Recipient = recipient;
        return this;
    }
 
    public Invoice Build()
    {
        return invoice;
    }
}
 
public class RecipientBuilder
{
    private Recipient recipient;
 
    public RecipientBuilder()
    {
        recipient = MockRepository.GenerateStub<Recipient>();
    }
 
    public RecipientBuilder WithAddress(Address address)
    {
        recipient.Address = address;
        return this;
    }
 
    public Recipient Build()
    {
        return recipient;
    }
}
 
public class AddressBuilder
{
    private Address address;
 
    public AddressBuilder()
    {
        address = MockRepository.GenerateStub<Address>();
    }
 
    public AddressBuilder WithNoPostalCode()
    {
        address.PostalCode = string.Empty;
        return this;
    }
 
    public Address Build()
    {
        return address;
    }
}

Once again be warned that this does not work for immutable objects, I don't recommend making value objects mutable just to implement this pattern so i'm not sure if it actually provides any real value. Maybe if it is combined with a mutable entity and a custom builder for a value object that preserves that value object's immutability. 

If anyone that reads this blog has a way to make Test Data Builders easier I would be glad to hear it.

 

[Currently Listening To: Young Galaxy- Young Galaxy - Wailing Wall]

Friday, February 08, 2008 6:56:17 AM (GMT Standard Time, UTC+00:00)  #    Comments [3]  | 
Tuesday, February 05, 2008

Justice asked a bunch of us to write a treasured memory that we have of Donald Belcham as a birthday tribute to Don. I am in a bit of a blogging slump so I figured I would oblige his request.

My memory is from DevTeach Vancouver last year. It all started at the Party with Palermo. Donald started the night off having a great conversation with a bunch of us about the merits of TDD and how it relates to life, the universe and everything.

Then someone told him that the booze was free.

Donald proceeded to order the finest scotch available in the bar. For those that don't know him Scotch to Donald is like Beer to Bender. Donald was ok but then Jeffery Palermo announced that booze would only be free for the next 5 minutes.

I was standing at the bar when the announcement was made and I heard Donald yell "LETS DO SHOTS!!!". Luckily Donald kept it classy and suggested that everyone do Prairie Fires (tequila and Tabasco). Knowing the dangers that Prairie Fires can bring, I tried to convince Donald that he should do something less harsh like Whiskey.

After 10 Prairie Fires everyone was amazed that Donald had proceeded to keep them down and we went for more drinks at The Earls on Robson Street. After 10 minutes I look over at down who is slumped over and hiccupping. 10 more minutes pass and Donald's cheeks are full with a finger holding in the gray liquid that eventually shot out into D'Arcy Lussier's hot chocolate (that whole sentence just sounded wrong).

DSCF1702

The next day Donald was scheduled to present and he lays out a great metaphor which to me, is pure Donald Belcham:

"A development shop without Cruise Control and NAnt is like having too many prairie fires here at DevTeach. Everything looks ok on the outside but sooner or later things are going to blow up."

PHENOMINAL!

If this blows your mind, make sure to check out Donald at this year's DevTeach Toronto. He will be presenting not one, count em 4 different presentations all likely to be fuelled by prairie fires and scotch.

Tuesday, February 05, 2008 8:20:52 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

Theme design by Jelle Druyts

Pick a theme: