[rspec-users] Need help mocking this out

Jarkko Laine jarkko at jlaine.net
Mon Dec 3 05:42:46 EST 2007

On 3.12.2007, at 11.57, Fischer, Daniel wrote:

> Let's say you're using the restful_authentication plugin.
> You have a model called articles. On the index action of the  
> articlescontroller you simply want to spec out that it'll scope the  
> results to the ownership of the current_user.
> It should NOT include any articles other than the articles that user  
> owns.

Given that you have

user has_many :articles

You want to use current_user.articles (or  
current_user.articles.find(*additional options*)).

In the spec, you want to make sure that the articles are scoped  
through the correct user:

before(:each) do
   @user = mock_model(User)
   User.stub!(:find).and_return(@user) # to make sure current_user is  
the mock model
   @articles = [mock_model(Article)]

it "should find articles for the current user" do
   get :index

It is then the responsibility of the articles association to scope the  
results. In the controller specs you just trust that it works.

Jarkko Laine

More information about the rspec-users mailing list