[rspec-users] Need help mocking this out

Pat Maddox pergesu at gmail.com
Sun Dec 16 22:00:46 EST 2007

On Dec 16, 2007 1:43 PM, David Chelimsky <dchelimsky at gmail.com> wrote:
> I tend to take this one more step. I'm a bit of a demeter zealot, and
> I'm also usually writing the examples from the code first. So I'd do
> something like:
> it "should find the current users articles" do
>   article = mock_model(Article)
>   current_user.should_receive(:find_article).with("1").and_return(Article)
>   get :show, :id => "1"
> end

I find that there are two problems with this approach:

- since proxies provide so much behavior, wrapping the proxy's methods
like this can quickly lead to a bloated API on User
- The Rails Way seems to care a lot less about demeter than you do.
Even though the method name is obvious, you still get people going,
"huh?  why doesn't he just do user.articles.find?"

I'm right there with you in theory, but in practice I've found that
people simply don't like it.  So I've just had to bite my tongue and
write slightly uglier specs (I won't give in on add_article, however.
articles.should_receive(:<<) gives me sharp stomach pains)

It has provided me a nice opportunity to express intent, though.
Whenever I want a default finder that does slightly more than
articles.find(1), I can write a find_article method to encapsulate it.
 Then when someone asks me, "well why don't use you juse
articles.find" I can reply that there's a little more than meets the
eye.  But how great is it that they don't have to know what it is in
order to use it!  Maybe there's something to this approach after all.

So like I said, in general I prefer your approach, but I've opted for
the principle of least surprise with my coworkers.  I think I make the
best of it by communicating those times when I depart from the
standard Rails chaining style.


More information about the rspec-users mailing list