Created
January 16, 2012 13:51
-
-
Save schneems/1620966 to your computer and use it in GitHub Desktop.
Devise Stub logged in user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include Devise::TestHelpers | |
# gives us the login_as(@user) method when request object is not present | |
include Warden::Test::Helpers | |
Warden.test_mode! | |
# Will run the given code as the user passed in | |
def as_user(user=nil, &block) | |
current_user = user || Factory.create(:user) | |
if request.present? | |
sign_in(current_user) | |
else | |
login_as(current_user, :scope => :user) | |
end | |
block.call if block.present? | |
return self | |
end | |
def as_visitor(user=nil, &block) | |
current_user = user || Factory.stub(:user) | |
if request.present? | |
sign_out(current_user) | |
else | |
logout(:user) | |
end | |
block.call if block.present? | |
return self | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment