[rspec-users] RSpec makes me want to write better code

Phlip phlip2005 at gmail.com
Tue Mar 31 08:19:45 EDT 2009

Fernando Perez wrote:

> 6 months since my initial post, what happened in between?
> - My controllers are getting anorexic, and that's good. An action 
> typically does only 1 call to a model and behind the scenes that model 
> will make other calls to other models and do some fancy stuff, this used 
> to happen in the controller which was bad. Now it has become so easy to 
> write model specs.

By "getting", do you mean new controllers arrive skinny? Or that you have 
refactored the same fat controllers, over time, until they are skinny?

The latter is preferred, because we should not be writing the same sites over 
and over again. In theory!

> - I don't spec controllers, because it's too painful to do. I don't spec 
> views either, because I tweak them too often, and it would take me more 
> time rewriting view specs then actually coding the views. If there is 
> something to test for is the rendering of the css across browsers, 
> because that's terrible!

We TDD, and refactor, the living shit out of controllers and views. Tools like 
assert_xhtml and form_test_helper make TDDing them easier than creating them by 

> - I use cucumber+webrat to test that some important public pages render 
> without a 500 error. I don't care if the view has such div except if it 
> is a critical one. What I mean is that I won't test, all the assignments 
> that should be on the page, as some tutorials demonstrate. This is 
> nearly impossible to maintain.

How often do you run your tests? Your editor should help you run them after 
every couple of edits. A lot of BDD is actually "cargo cult" "Test-Driven 
Development" (Google the terms independently to see what I mean).

Someone would get the benefits you describe doing just some of the TDD cycle, 
but if they left out other parts they might blame the views for the remaining 

> - I can refactor my code, immediately spot areas where my code got 
> broken, and fix it. Before some parts of my app would be broken without 
> having me knowing about it for a long time until some cool customers 
> report a few bugs, but this was not an acceptable situation.

Right: Your car now has headlights, _and_ brakes. This means you can go faster.

> - I don't use Autotest, it sucks too much power, and it is too much 
> distracting. From time to time I run specs to check if things look good, 
> and always before updating the code on the server.

But you should _want_ to test every few edits. More important, you should be 
able to correctly predict the result of each run.


More information about the rspec-users mailing list