Rails, Fast Tests and Architecture

For a while now I have been flirting with Ruby and Ruby on Rails. Very early on, I learned the hard way to stick to the ‘Rails way’, especially when you don’t yet understand the technology. I have however, often found myself fighting both rails and what has appeared to be the ‘standard’ way of doing things in the Rails community.

While the Ruby / Rails community have been writing tests, and lots of them, the .net community has been catching up. In that time though, writing tests in .net, I’ve concentrated on the speed they run, we all want fast feedback right? Lets push those slow running full stack tests away from the unit tests so we don’t need to run them all the time, and while we’re at it lets minimise how many there are, lets just make sure the whole thing hangs together, lets not test every permutation through the UI while hitting the DB. Starting out with Rails, this was one of my first pain points, the lack of isolation in the test suite.

For a while I wondered why every test I wrote needs to spin up the entire stack, it’s so slow. Putting all my domain logic on the ActiveRecord bound models, my model tests are slow as they go to the database all the time. But this seems to have been where to put your logic for quite a while, after all, we want ‘fat models and thin controllers’. Does this really mean though that the entire model should be in one class?

Lets think about Rails another way, what if all ‘model’ means is ‘the application’. What if the ‘Rails Application’ isn’t the application at all? Maybe it’s just a presentation adapter. Well maybe not, there’s ActiveRecord too! So maybe Rails is two things, a presentation adapter and a persistence adapter.

I thought I was either going mad, or inventing a problem. I’m not experienced with the framework, maybe I’m missing something. Why don’t these ‘Rails guys’ care about this stuff, is everything I know about OO going out the window? So I put down Rails, continued playing with Ruby, and kept building stuff in .net. And then I picked up Rails again and discovered that people have been thinking about this stuff. I discovered Corey Haines Fast Rails Tests talk. Uncle Bob’s been fighting the corner and I recently came across his talk Architecture The Lost Years, and recently, Avdi Grimm’s been writing a book Objects on Rails which you have to read.

So where does this leave us? When should we worry about the architecture, and when will all this stuff help, might it be a hinderance? I’ll write about this in a future post (hopefully not too far in the future!)