I have sinatra appliction using session, how do I test a page that uses session?
I using Rspec + sinatra
Tks
This page, show's the main problem of testing session with rspec and sinatra.
The main problem is that the variable session is not accessible from the test code. To solve it, it suggests (and I confirm) to add the fowling lines at spec/spec_helper.rb:
def session
last_request.env['rack.session']
end
It will make accessible a mocked session object from the last request at the test code.
Here's my controller code that read's a code param and stores its value at session:
post '/step2callback' do
session['code'] = params['code']
end
Then here's test code:
context "making an authentication" do
before do
post "/step2callback", code: "code_sample"
end
it "reads the param code and saves it at session" do
expect(session['code']).to eq("code_sample")
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With