[rspec-users] spec'ing controllers
matt at mattwynne.net
Wed Dec 16 12:31:04 EST 2009
On 16 Dec 2009, at 16:56, Stefan Kanev wrote:
> 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
> The RSpec book suggests mocking models and not integrating vies
> controller specs. Following that, I usually end up with something
> like this: http://gist.github.com/257946
> 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,
Sounds like you've got it just about right to me :)
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.
matt at mattwynne.net
More information about the rspec-users