Spec'ing controllers

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,
Stefan

On 16 Dec 2009, at 16:56, Stefan K. wrote:

like this: topics_controller.rb · GitHub
• Small changes in models (or views) don’t result in broken
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,
Stefan

Sounds like you’ve got it just about right to me :slight_smile:

I think the excess overhead thing will pass. Partly because you’ll get
faster at it, but also partly as your design skill improves (guided by
mocks) you’ll feel less need to drive out every single change with a
unit test, and rely a little more on Cucumber. Your observation about
specs passing when mocks are wrong is a common one, and one I remember
struggling with when I first learned TDD, but again as you learn to
trust Cucumber you’ll find that this is OK.

cheers,
Matt

+447974 430184
[email protected]

http://archive.patmaddox.com/blog/2009/1/15/how-i-test-controllers-2009-remix
is my take on things. Due for an update though looks like :slight_smile: