Wednesday, May 31, 2006

Taking things into consideration

I've been working on a new style of writing tests for a while. One of thing we're finding at the moment, is that you often want to narrow down the available objects to pick out a particular part, and then make a number of assertions about this same part.

For example, you might be modelling a garden, and you want to write some tests concerning a certain set of plants, maybe those that are yellow. Using the style that Tom White and I have discussed before, we could write something like:

assertThat(garden, has(flowers().coloured(yellow)));
assertThat(garden, has(flowers().in(flowerbed()).coloured(yellow)));

This checks that there exist some flowers that are yellow. But what if we want to check that all the flowers in the flowerbed are yellow? We've come up with the idea of a consideration. We use a considering(...) clause to refine the scope of our assertion, and have also created an assertThatAll() assertion to take into account the universal quantification. The results look like this:

assertThatAll(flowers(), areColoured(yellow));
assertThatAll(flowers(), areInBloom());

I think this will be subject to some refinement as we put it to use in more places, and we'll see whether or not it is useful. We've certainly found a few places so far where we want to check a property for a set of objects, and writing a loop isn't very in keeping with our literate style.

<< Home

This page is powered by Blogger. Isn't yours?