The accepted answer suggests rack_session_access. It works by inserting middleware controllers to edit and update the session state, then has capybara visit that page and submit a form with the session data. Very ingenious! But unnecessary if you are using Warden (directly or through Devise).
Warden has a hook on_next_request that gives access to the warden mechanism, which can be used to set session keys directly. I threw this together to bundle it up in rspec:
Create spec/support/inject_session.rb:
module InjectSession
include Warden::Test::Helpers
def inject_session(hash)
Warden.on_next_request do |proxy|
hash.each do |key, value|
proxy.raw_session[key] = value
end
end
end
end
In spec/spec_helper.rb include the module in feature specs:
RSpec.configure do |config|
config.include InjectSession, :type => :feature
end
Then sample use in a spec might be:
inject_session :magic => 'pixie dust', :color => 'pink'
visit shopping_cart_path
page.should be_all_sparkly_and_pink # or whatever