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

Fernando Perez lists at ruby-forum.com
Tue Mar 31 05:39:16 EDT 2009

Fernando Perez wrote:
> Hi,
> Today is a big day. I officially transitioned from manually testing by
> clicking around in my app, to automated testing with RSpec + Autotest.

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.

- 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!

- 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.

- 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.

- 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.

- I have written my own Factory. It's not OOP, it could be refactored, 
but it works 100% as advertised, provides default values, can handle 
associations, etc. I am pleased with it (I tried factory girl, 
machinist, etc and got pissed off). I encourage anyone to write his own 
little factory, to get a better hang of how to write good specs. I 
totally got mad with fixtures, it is impossible to maintain specs/tests 
with fixtures. Impossible.

- I don't do true BDD, i.e: I still write code before specs, because 
that's what motivates me. I consider that seeing my app living and 
writing code for my app more important than writing specs. Specs are 
still important, but only as a bug reporting tool, they don't add any 
value for the customer. In this crisis If I wanted an employee to resign 
by himself without paying him benefits (that's how it works in Europe), 
I would make him write specs all day long, and forbid him from seeing 
the app and play with it. He wouldn't last 1 week doing this.

What about you?

Best regards,
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list