Created
January 13, 2015 20:44
-
-
Save marcioj/99ef27ab8baff6bac850 to your computer and use it in GitHub Desktop.
Devise rspec controller macro
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
# Taken from https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec), | |
# with some minor changes | |
module ControllerMacros | |
def login_admin(&proc) | |
before(:each) do | |
@request.env["devise.mapping"] = Devise.mappings[:admin] | |
admin_user = proc ? instance_eval(&proc): FactoryGirl.create(:admin_user) | |
sign_in admin_user | |
end | |
end | |
def login_user(&proc) | |
before(:each) do | |
@request.env["devise.mapping"] = Devise.mappings[:user] | |
user = proc ? instance_eval(&proc): FactoryGirl.create(:user) | |
# user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module | |
sign_in user | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.extend ControllerMacros, :type => :controller | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment