Created
August 7, 2011 10:59
-
-
Save kevinrutherford/1130290 to your computer and use it in GitHub Desktop.
Mocking warden in controller specs
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
module RandomHelpers | |
def random_number() SecureRandom.random_number(10000); end | |
def random_id() random_number; end | |
def random_name() SecureRandom.hex(20); end | |
end | |
module ControllerHelpers | |
def user_double(attrs = {}) | |
user_attrs = { | |
:first_name => random_name, | |
:last_name => random_name, | |
:authenticatable_salt => 'x' | |
}.merge(attrs) | |
mock_model(User, user_attrs) | |
end | |
def stub_sign_in_with(user) | |
request.env['warden'] = double(Warden, | |
:authenticate => user, | |
:authenticate! => user, | |
:authenticate? => true) | |
sign_in(user) | |
return user | |
end | |
def stub_sign_in(attrs = {}) | |
stub_sign_in_with user_double(attrs) | |
end | |
end | |
RSpec.configure do |config| | |
config.include RandomHelpers | |
config.include Devise::TestHelpers, :type => :controller | |
config.include ControllerHelpers, :type => :controller | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment