[rspec-users] spec'ing controllers

Stefan Kanev stefan.kanev at gmail.com
Wed Dec 16 11:56:46 EST 2009

Hey guys.

I switched completely to RSpec and Cucumber this spring and I am really
happy with. While I think I've gotten quite good with it, I'm not sure I
understand the value of spec'ing controllers (in Rails). I would appreciate
if you can give me some suggestions. Let me elaborate:

The RSpec book suggests mocking models and not integrating vies controller
specs. Following that, I usually end up with something like this:

I see a few benefits:

   - It helps keeping the controllers simple. For example, while writing
   that spec I discovered that Topic should have #update_topic_hits (as opposed
   to inlining it there). That improved the design.
   - When mocking gets tricky, I treat this as a code smell and start
   refactoring. Usually it works out well.
   - Specs are faster. Remembering how much time it took to run the
   functional tests on an old project, I can really appreciate that.
   - Small changes in models (or views) don't result in broken controller
   specs. It took some time, but I can see it now.

However, I still don't feel too confident about the way I'm doing it. Some
of my worries:

   - It feels like lots and lots of code. I'm normally at 2:1 test:code, but
   it feels I'm overdoing it. *feels* is the key word here -- I might be
   worrying too much.
   - I have a similar spec for each RESTful controller. Looks like
   duplication, but I'm not sure what to do about it
   - Often the specs are passing when the code is broken (due to the mocks).
   While Cucumber catches those errors for me, I'm not sure if they shouldn't
   be in the specs in some way.
   - It's a lot of effort. Eventually ones gets used to it, but I can't
   decide whether it pays of or not.

Can you validate (or invalidate) my reasoning? It will be very nice to hear
a comment from more experienced rspec users. Comments on the the spec I
posted would be appreciated too, of course.

Thanks very much,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091216/89dd585b/attachment.html>

More information about the rspec-users mailing list