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


it's first time i'm working sinatra , can't sessions work in tests. have enable :sessions in app.

i tried:

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

or

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

but no session variables being set in request. i've looked around web , tried several suggestions nothing seems work. missing something?

thanks!

rack doesn't support passing in sessions via request anymore (rack >= v1.0). read post more detailed information on that.

the best way set session variable in app call action inside of application set session variable. instance, if have route inside app sets session variable this:

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

let's pretend there's route wanted test using session variable this:

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

then in tests, should first call route sets session, go onto route uses it. here rspec notation, since use testing:

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

if going using route in tests, write method accomplish in test file (if you're using rspec, method go in spec_helper.rb or in controller_spec.rb file):

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

and call in tests when needed set:

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

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -