Topic: Stubbing a protected method (Solved)

Is stubbing a protected method possible?  Here's what I'm doing:

#Inside room_controller_spec.rb, up in the before:
@participant = mock("participant")
add_stubs(@participant, :email => 'participant', :password => 'participant', :class => Participant, :room_id => 1)

#one of my test cases
it "should redirect participants to their room when they reach /rooms/" do

    #:current_user is actually defined in another location:
    # RoomsController < ApplicationController < AuthenticatedSystem
    RoomsController.stub!(:current_user).and_return(@participant)
    RoomsController.stub!(:current_user=).and_return(@participant)
    RoomsController.stub!(:logged_in?).and_return(true)
   
    puts @controller.current_user
   
    post :index
   
    response.should redirect_to(room_path(@participant.room))
end

#In AuthenticatedSystem:
    def current_user
      #@current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false)
      @current_user ||= (login_from_session || :false)
    end
   
    # Store the given user in the session.
    def current_user=(new_user)
      if new_user.nil? || new_user.is_a?(Symbol)
        session[:user] = nil
        session[:user_type] = nil
      else
        session[:user] = new_user.id
        session[:user_type] = new_user.class.name
      end
      @current_user = new_user
    end


The line "puts @controller.current_user" shows me that current_user is nil.  Is it not possible for me to stub this or is there another way to do it correctly?  Thanks!

Last edited by GlennFord (2007-11-05 17:18:23)

Re: Stubbing a protected method (Solved)

Voxdolo found my problem in #rspec (irc.freenode.net) and pointed out (what seems so simple now) that I should replace:

RoomsController.stub!(:current_user).and_return(@participant)
RoomsController.stub!(:current_user=).and_return(@participant)

with
controller.stub!(:current_user).and_return(@participant)

Problem solved!  Thanks voxdolo

Last edited by GlennFord (2007-11-05 17:23:37)