[rspec-users] Stubbing Login Stuff

s.ross cwdinfo at gmail.com
Sun Apr 15 14:04:20 EDT 2007


For some reason, I'm completely blanking on how to do this... Sorry  
for the lots-o-code example.

What I'm trying to do: stub the validate_login method in  
application_controller so it returns true (user is logged in).
What happens: validate_login method in application_controller.rb is  
called instead of my stub.

What I'm struggling with is the syntax to make the stub override the  
protected method in the parent class.

Thanks,

application.rb

   def requires_authorization
     render(:template => 'shared/login') unless validate_login
   end

   protected
   def validate_login
     return false if session[:user].blank? || session[:user] !=  
LOGIN_USER_NAME		# oversimplification, be gentle
     return true
   end

admin_controller.rb

class AdminController < ApplicationController
   before_filter :requires_authorization
   def index
   end
end

admin_controller_spec.rb

describe "an AdminController should" do
   controller_name :admin
   integrate_views

   it "should grant access to users who are logged in" do
     stub!(:validate_login).and_return(true)
     # also tried ApplicationController.stub! 
(:validate_login).and_return(true). Same result.
     get :index
     response.should be_success
     puts response.body
     response.should have_tag('h1', 'Administer Site')
   end
end




More information about the rspec-users mailing list