Sinatra + Rack :: Test + Rspec2 - Using Sessions?

This is the first time I work with Sinatra, and I just can't get sessions to work in my tests. I have enable :sessionsin my application.

I tried:

get "/controller/something", {}, "rack.session" => {:session => "Aa"}

or

get "/controller/something", {}, "session" => {:session => "Aa"}

But in my request session variables are not set. I went through the website and tried some suggestions, but nothing works. Did I miss something?

Thank!

+5
source share
2 answers

The rack does not support on-demand session transfer anymore (Rack> = v1.0). Read this post for more information on this.

- , . , , :

post '/set_sess_var/:id'
  session[:user_id] = params[:id]
end

, , , :

get '/get_user_attributes'
  User.find(session[:user_id]).attributes
end

, , , . rspec, , :

it "should print out user attributes" do
  user_id = 1
  post '/set_sess_var/' + user_id
  get '/get_user_attributes'
  last_response.body.should == User.find(user_id).attributes
end

, ( Rspec, spec_helper.rb controller_spec.rb):

def set_session_var(user_id)
  post '/set_sess_var/' + user_id
end

, :

it "should print out user attributes" do
  set_session_var(1)
  get '/get_user_attributes'
  last_response.body.should == User.find(1).attributes
end
+3

, env:

get "/controller/something", {}, "rack.session" => {:session => "Aa"}
+1

All Articles