Here's how I test my admin controllers that use HTTP basic authentication using RSpec 2:
before(:each) do user = 'test' pw = 'test_pw' request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) end
Actually, that's how I did it when I first tested things, but I've since put it in its own module under spec/support/auth_helper:
module AuthHelper # do admin login def admin_login user = 'test' pw = 'test_pw' request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) end end
and now my controller spec looks like this:
describe Admin::LocationsController do include AuthHelper before(:each) do admin_login end describe "GET index" do it "assigns all locations as @locations" do loc = Factory.create(:location) get :index assigns(:locations).should eq([loc]) end end describe "GET show" do it "assigns the requested location as @location" do loc = Factory.create(:location) get :show, :id => loc.id assigns(:location).should === loc end end end
That "Factory" line comes from my use of factory_girl rather than fixtures.