[rspec-users] Nosy controller specs

Mark Wilden mark at mwilden.com
Thu Dec 11 12:36:34 EST 2008

On Thu, Dec 11, 2008 at 4:33 AM, Andrew Premdas <apremdas at gmail.com> wrote:

>>     it "should find all posts" do
>>       Post.should_receive(:find).with(:all).and_return([@post])
>>       do_get
>>     end
>> Now this last spec "should find all posts" is nosy is far as I'm concerned
>> in that its specifying how the model should get all the posts (i.e. white
>> box testing) rather than checking the result i.e. that its got @post back.
>> So now if I change the model so that "all posts" is for example "all posts
>> in last year" because there is a new business rule that we don't show posts
>> over a year old then my controller spec fails.
I think this is probably correct as is.

When specing a controller, it's correct BDD to specify how it interacts with
other objects. If the controller itself wants all posts in the last year,
it's OK to test that. The fact that only this year's posts are of interest
is a presentation issue - it's not an essential characteristic of the data.
It's not the model's job to decide that all clients should get back posts in
the last year, and hence that's what its find method should return.

All business rules don't reside in the model. As soon as you say "we don't
SHOW posts over a year," you're talking about a presentation rule, not a
model rule. Controllers mediate between data and screen - they're
responsible for getting the data to be shown. That code should exist in the
controller and be tested there.

On the other hand, the controller should not necessarily be responsible for
defining what constitutes "all posts in the last year." That should very
likely be a named scope.

This is all just my humble opinion, of course, and might be utter rubbish in
any particular real world situation.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081211/c4b63fb6/attachment.html>

More information about the rspec-users mailing list