How to test a Sinatra application using a session
It seems like the problem is to activate enable :sessions.
You must deactivate this option to be available to overwrite the session.
The solution may be:
# my_test.rb (first line, or at least before you require your 'my_app.rb')
ENV['RACK_ENV'] = 'test'
# my_app.rb (your sinatra application)
enable :sessions unless test?
# my_test.rb (in your test block)
get '/', {}, 'rack.session' => { :key => 'value' }
On the other hand, in order to be able to check any session change that is expected in action, we can send not a hash to rack.session, but a pointer to a hash so that we can check after calling the action if the hash has changed:
# my_test.rb (in your test block)
session = {}
get '/', {}, 'rack.session' => session
assert_equal 'value', session[:key]
, - , , @fguillen . , / , , , :
# in myapp_spec.rb
require 'rspec'
require 'rack/test'
require 'myapp'
it "should set the session params" do
get 'users/current/projects', {}, 'rack.session' => {:user =>'1234'}
end
# in myapp.rb
enable :sessions
get 'users/current/projects' do
p env['rack.session']
end