Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Last active December 21, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanbehan/6237307 to your computer and use it in GitHub Desktop.
Save seanbehan/6237307 to your computer and use it in GitHub Desktop.
Testing Sinatra current_user sessions
require 'sinatra'
configure do
enable :sessions
end
get '/' do
if session[:user_id]
"OK"
else
raise "NOT OK!"
end
end
require 'test/unit'
require 'rack/test'
require 'app'
class CurrentUserSession < Test::Unit::TestCase
include Rack::Test::Methods
def app
Sinatra::Application
end
def current_user_session
get '/', {}, { 'rack.session' => { user_id: current_user.id } }
end
def current_user
User.find_or_create_by(full_name: "Sean Behan")
end
def setup
current_user_session
end
end
class UserTest < CurrentUserSession
def setup
# Remember to call super...
super
end
def test_index
get "/"
assert_equal current_user.id, last_request.env['rack.session']['user_id']
assert last_response.ok?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment