Skip to content

Instantly share code, notes, and snippets.

@troelskn
Created February 27, 2013 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troelskn/5047135 to your computer and use it in GitHub Desktop.
Save troelskn/5047135 to your computer and use it in GitHub Desktop.
Mock out sessions with Rack::Test
require File.join(File.dirname(__FILE__), 'test_helper.rb')
class AppTest < Test::Unit::TestCase
def test_get_root_path
current_session.rack_session[:user_id] = "1337"
get "/"
assert last_response.ok?
end
end
module Rack
module Test
class Session
alias_method :old_env_for, :env_for
def rack_session
@rack_session ||= {}
end
def rack_session=(hash)
@rack_session = hash
end
def env_for(path, env)
old_env_for(path, env).merge({'rack.session' => rack_session})
end
end
end
end
@christiankakesa
Copy link

Thank you, I use Rack::Session::Redis in my application and adding this patch in my test_helper.rb save my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment