简体   繁体   中英

Testing for session variables using Rack::Test using RSpec

Given the code

env "rack.session", {:var => 'value'}

I can set a session variable in rack-test. However, how can I (using rack-test) test for said session variable in RSpec? Hypothetically, for example:

expect(env['rack.session'][:var]).to eq('value')

I can't seem to find any documentation for reading Rack environment variables, only writing them.

Just use the session method on the last request:

last_request.session

This will give you the session hash. A Sample:

it 'allows to access the session' do                                                                                                                                          
  get '/'                                                                                                                                                                     
  session = last_request.session                                                                                                                                              
  expect(session).to be_a Hash                                                                                                                                                
  expect(session[:var]).to eq 'value'                                                                                                                                         
end

I've tested with rack v1.6.5 and rack-test v0.6.3 but it should work as well with rack version 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM