[rspec-users] Nosy controller specs

Yi Wen hayafirst at gmail.com
Fri Dec 12 00:01:51 EST 2008

yeah, instead of using Post.find(:all) in your controller, use a method like
Post.all_posts, or some names like that. Then in the controller spec, you
Post.should_receive(:all_posts) and leave the implementation detail of this
method to the Post model. Technically speaking, this all_posts more belongs
to Service layer though.


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

> Looking at generated controller specs we tend to get something like
> describe PostsController do
>   describe "handling GET /posts" do
>     before(:each) do
>       @post = mock_model(Post)
>       Post.stub!(:find).and_return([@post])
>     end
>     def do_get
>       get :index
>     end
>     ...
>     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. Now it seems to me that I
> should only have to change my model specs when I make this sort of change,
> this implementation details is none of the controllers business
> So a couple of things
> 1) Am I right about this?
> 2) If so is there a better way to still use the mock for speed but not be
> nosy
> 3) Should the default controller generators be re-written
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081211/1ab9ce27/attachment.html>

More information about the rspec-users mailing list