[rspec-users] how to stub the :current_user and any methods associated with it

Jarkko Laine jarkko at jlaine.net
Fri Apr 11 03:21:04 EDT 2008


On 11.4.2008, at 4.57, newbie wrote:

> Thanks for the direction. I'm glad to hear that I'm going in the right
> direction. I just have a final question and I seem to be hitting a
> stub/mocking road block at the moment. In my controller I have this...
>
> def index
> @entries = current_user.entries
> end
>
> How would I rspec this out correctly? This is what I have but it looks
> wrong.
>
>  before(:each) do
>    @current_user = mock_model(User, :id => 1)
>    @entry = mock_model(Entry, :user_id => 1)
>    controller.stub!(:current_user).and_return(@current_user)
>    controller.stub!(:entries).and_return(@entry)
>  end
>
>
>  describe "on the index page" do
>    it "should show all of the current_users entries" do
> 	
> @current_user.should_receive(:entries).with(@entry).and_return(true)

This is wrong. The with method means parameters the expected method  
call receives. In your case it gets no parameters. Besides, since you  
stub the entries method anyway, you can just specify the behaviour.

What you probably want with the code above is this:

assigns[:entries].should == @entry

//jarkko

>
>    end
>  end
>
>
>
>
>
>
>
>
>
>
> On Apr 7, 11:31 am, "Bryan Ray" <bryans... at gmail.com> wrote:
>> Responses shown below...
>>
>> Sorry this isn't extremely informative ... in a bit of a rush.  
>> Hopefully it
>> will point you in the appropriate direction.
>>
>>
>>
>> On Sun, Apr 6, 2008 at 2:17 PM, newbie <alen... at gmail.com> wrote:
>>> I'm new to Rspec and I'm having a bit of trouble with this  
>>> controller
>>> that I'm testing. I think I'm doing it right but I guess my syntax  
>>> is
>>> wrong.
>>> I'm currently using the "acts_as_authenticated" plug in.
>>
>>> What I want to test out is
>>
>>> EventController on entering the tickets_page
>>> - should show the tickets_page  if the current_user has NOT entered
>>> this page today
>>
>>> -----------------------------------------------------
>>> Below is my event controller
>>> -----------------------------------------------------
>>
>>> class EventController < ApplicationController
>>
>>>  before_filter :login_required
>>
>>>  def tickets_page
>>>      if current_user.has_already_entered_today?
>>>        flash[:notice] = 'Come back tomorrow'
>>>        redirect_to :action => 'home'
>>>      else
>>>        flash[:notice] = 'Welcome'
>>>      end
>>>  end
>>> end
>>
>>> -----------------------------------------------------
>>> Below is my rspec for this controller
>>> -----------------------------------------------------
>>
>>> require File.dirname(__FILE__) + '/../spec_helper'
>>> describe EventController do
>>
>>>  before(:each) do
>>>    @current_user = mock_model(User, :id => 1)
>>>    controller.stub!(:current_user).and_return(@current_user)
>>>    controller.stub!(:login_required).and_return(:true)
>>>  end
>>
>>>  fixtures :users
>>
>>>  describe "on entering the tickets page" do
>>>    it "should show the tickets_page  if the current_user has NOT
>>> entered this page today" do
>>>     controller.current_user.stub!
>>> (:has_already_entered_today?).and_return(:false)
>>>     get :tickets_page
>>
>>> controller 
>>> .current_user.should_receive(:has_already_entered_today?).with(:f  
>>> alse).and_return(:false)
>>>     response.should render_template(:tickets_page)
>>>    end
>>> end
>>
>>> end
>>
>>> -----------------------------------------------------
>>> My errors
>>> -----------------------------------------------------
>>> Mock 'User_1001' received unexpected
>>> message :has_already_entered_today? with (no args)
>>
>> So this is a good start ... it means that the controller action is  
>> actually
>> being mocked out ... you just need to keep moving forward mocking/ 
>> stubbing
>> out the rest of your controller actions. it's complaining that it  
>> doesn't
>> know what to do on your *current_user.has_already_entered_today?*  
>> method.
>>
>> @current_user.stub!(:has_already_entered_today?).and_return(<insert  
>> true or
>> false here>)
>>
>>
>>
>>> Anyone can help with any direction of what I might be doing wrong?
>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-us... at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> --
>> Bryan Rayhttp://www.bryanray.net
>>
>> "Programming today is a race between software engineers striving to  
>> build
>> bigger and better idiot-proof programs, and the Universe trying to  
>> produce
>> bigger and better idiots. So far, the Universe is winning."
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080411/1b9e1afb/attachment-0001.bin 


More information about the rspec-users mailing list