TroubleThere are some cases when you may need to access application session in your tests:
- User signup or registration flow is too long.
- Application use another backend and store result into session.
And it’s true :)
Access to session and request is not possible from the test ...
RescueSo is it actually possible to access session? Actually yes :)Instead of trying to hack capybara you may just extend your application in test environment! Just add some code that modify session according to given request parameters.If you use rack based application then your are lucky – try rack_session_access gem.If it’s another application you need implement concept yourself :)This gem should work with any rack application. We covered it with acceptance testing against:
- rack builder application
- sinatra application
- rails3 application
See README for usage examples.
$ gem install rack_session_access
Rails + Rspec + Capybara exampleAdd to spec/spec_helper.rb:
And use set_rack_session helper in acceptance test:
feature "My feature" do
@user = Factory(:user)
scenario "logged in user goes to profile page" do
# read your authorization engine manual how it store user into session
page.set_rack_session(:user_id => @user.id)