Friday, May 25, 2007

contexts, test/spec and ruby on rails




For this new Ruby on Rails project I'm coding, I'm doing The Right Thing and am writing specifications for everything before or during the code writing process. I'm also making the application really model heavy, as I have found out the hard way that testing or speccing controllers is quite a bit more tricky than just adding more methods to your models. Model heavy applications is also probably The Right Thing, as it makes your controller code easier to understand, and moves functions to smaller Model methods.

I'm using test/spec, but I was quite confused about exactly what a "context" is. Here's a great definition from Luke Redpaths blog:


"Behaviour Driven Development favours the breaking up of specifications into individual ?contexts?. A context is
an object (or collection of objects, but generally object being specced) in a certain state."

This makes a lot of sense, since the "setup" function is within each "context". So, if you were coding an application, you would break up your unit tests into a bunch of different contexts, each of which would have a different set of model objects. Perhaps one for your most general tests, another to make sure your validations are all working, and another to test your business logic on a smaller subset of fixtures or hand built models.