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

newbie alen198 at gmail.com
Sun Apr 6 15:17:44 EDT 2008


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)


Anyone can help with any direction of what I might be doing wrong?







More information about the rspec-users mailing list