VULPINE
VULPINE

Reputation: 31

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.

Upvotes: 3

Views: 1473

Answers (1)

Björn Albers
Björn Albers

Reputation: 323

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.

Upvotes: 5

Related Questions