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

Bryan Ray bryansray at gmail.com
Mon Apr 7 11:31:43 EDT 2008


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 <alen198 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(:false).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-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Bryan Ray
http://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."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080407/3fc1fc06/attachment.html 


More information about the rspec-users mailing list