Skip to content

Instantly share code, notes, and snippets.

@dhulihan
Created February 14, 2015 01:29
Show Gist options
  • Select an option

  • Save dhulihan/494284bd4a87a5b17b23 to your computer and use it in GitHub Desktop.

Select an option

Save dhulihan/494284bd4a87a5b17b23 to your computer and use it in GitHub Desktop.
RSpec 3 + FactoryGirl Login Stubs
# support/login.rb
module RSpec
module Core
class ExampleGroup
def login_admin
u = FactoryGirl.create(:admin)
login_as(u)
allow_any_instance_of(ApplicationController).to receive(:check_permissions).and_return(true)
end
def login_user
u = FactoryGirl.create(:user)
group = FactoryGirl.create(:group, :name => "#{u.to_s}'s Group")
login_as(u)
end
def login_as(user)
configure_request
# Stub on Class Level for capybara, since it doesn't use @controller
if described_class
allow_any_instance_of(described_class).to receive(:authenticate_user).and_return(true)
allow_any_instance_of(described_class).to receive(:current_user).and_return(user)
end
end
def login_anonymous
configure_request
end
def current_user
@controller.send(:current_user)
end
def bypass_captcha
allow_any_instance_of(ApplicationController).to receive(:human?).and_return(true)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment