Tangential comments about Software Development

Monday, June 28, 2010

Build your test data

My new favourite line of code is return this;

Credit for this is due to Steve Freeman and Nat Pryce, for Chapter 22 of their book Growing Object-Oriented Software, Guided By Tests, chapter title Constructing Complex Test Data.

It's been beyond my skills to write tests for systems like CMR.Net which are heavily data dependent. So the data builder approach in Steve and Nat's book has changed everything. Here's how a test now appears.

public void Formula()
MicroGrid mg = new GridBuilder()
.WithTextCell("A1", "2")
.WithFormulaCell("A2", "=A1*2")
MGCell c = mg.CellAt("A2");
Assert.AreEqual(CellTypeLetter.FORMULA, c.CellType);
Assert.AreEqual("4", c.DisplayValue);

It's that chained set of statements, starting with a new GridBuilder object, whose WithXX functions add internal data and return 'this'. They can therefore be chained together, until a final call to Build() returns a MicroGrid object. Damn fine.