Skip to content

Instantly share code, notes, and snippets.

@mikew
Created October 27, 2009 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikew/219330 to your computer and use it in GitHub Desktop.
Save mikew/219330 to your computer and use it in GitHub Desktop.
class ActionController::TestCase
# setup :mock_warden
teardown :cleanup_warden_env
def warden
@_warden ||= mock
end
def mock_warden_for(*scopes)
scopes.each do |scope|
warden.stubs(:authenticate!).with(:scope => scope) # before_filter :authenticate_user!
end
@controller.request.env['warden'] = warden
end
def mock_warden
mock_warden_for :user
end
def sign_in_as(people)
mock_warden_for *people.keys
people.each_pair do |scope, object|
warden.stubs(:user).with(scope).returns(object) # current_user
warden.stubs(:authenticated?).with(scope).returns(!object.nil?) # user_signed_in?
end
end
def sign_in
sign_in_as :user => Factory(:user)
end
private
def cleanup_warden_env
@controller.request.env.delete('warden')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment