[rspec-users] mocking named_scope utilization

Pat Maddox pergesu at gmail.com
Fri Sep 12 10:32:54 EDT 2008

On Fri, Sep 12, 2008 at 5:20 AM, Juanma Cervera <lists at ruby-forum.com> wrote:
> Hello,
> I am having trouble to mock the chaining of named_scope in the
> controller, also I would like to use will_paginate.
> def index
>  @things = Thing.allowed_for(@current_user).available.paginate :page =>
> params[:page]
> end
> ¿How to do it?
> Thanks

You're basically just going to have to chain some stubs.

@available = stub("available things", :paginate => [:thing])
@allowed_for = stub("allowed things", :available => @available)
Thing.stub!(:allowed_for).and_return @allowed_for

I would probably wrap that chain up in a method though, that expresses
what you want and makes it easier to stub

class Thing
  def self.available_to(user)

This makes your test become

@available = stub("available things", :paginate => [:thing])
Thing.stub!(:available_to).and_return @available

and to mock it, you can do

Thing.should_receive(:allowed_for).with(mock_user).and_return @available


More information about the rspec-users mailing list