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

Mark Wilden mark at mwilden.com
Thu Sep 25 12:48:54 EDT 2008

On Thu, Sep 25, 2008 at 8:01 AM, Fernando Perez <lists at ruby-forum.com>wrote:

> Also, RSpec made me discover something else: my model has some custom
> find methods. Often over time I find myself changing the name of these
> custom find  methods, e.g: find_all_products -> find_available_products
> As some of these finds are used by more than 1 controller, changing the
> name of the find_ often breaks code in different places at once. This
> was painful to manually test, and it is still a bit painful to have
> RSpec test, as I would also have to rename the custom find method in my
> specs.

Didn't find_all_products do something different from
find_available_products? If so, it's not really a renaming issue, as far as
I can see. You're changing behavior, therefore you change the specs.

> Based on this recent article:
> http://www.matthewpaulmoore.com/articles/1276-ruby-on-rails-code-quality-checklist
> "Each controller action only calls one model method other than an
> initial find or new.".

I didn't get that article (or, rather, that particular subarticle) at all.
It sounded to me like some of the virulent REST advocates who want to
shoe-horn every action into an HTTP verb. The controller should do what the
controller needs to do. If that means displaying just available products
instead of all products, it should do that "with intention," not just as the
default find action. That said, scopes can and should be defined, but that's
a well-understood idiom of Rails programming and can lead to abuse.

Would this mean that I should change my custom find naming convention to

> something more general that would never have to be renamed over time
> such as find_for_index, find_for_create, etc?

That puts too much controller knowledge in the model, in my opinion.

> Also, is it clever to write specs such as:
> --
> Product.respond_to? :find_for_index
> --
> so that if I break the rule of my naming convention, one of my spec will
> quickly bark at me.

I don't think it really matters whether Product has a find_for_index method
in and of itself. What matters is that the desired behavior, possibly
including and/or implemented by calling find_for_index, is achieved. If such
a naming convention was very important to the project, then a spec like that
would be appropriate. However, I'd regard it as a code smell if I saw such a

I can imagine other opinions on this subject and am eager to hear them.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080925/793ceb1a/attachment-0001.html>

More information about the rspec-users mailing list