Skip to content

Instantly share code, notes, and snippets.

@tomsabin
Last active September 7, 2018 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomsabin/652be7272c9829bde9f4409e95ae57bd to your computer and use it in GitHub Desktop.
Save tomsabin/652be7272c9829bde9f4409e95ae57bd to your computer and use it in GitHub Desktop.
Custom RSpec helper `in_another_session` or `in_session(:name)` to run expectations within different browser sessions
RSpec.feature "Welcome message" do
sign_in
visit "/"
expect(page).to have_content "Hello Joe Bloggs"
in_another_session do
visit "/"
expect(page).to have_content "Welcome"
end
end
RSpec.feature "Welcome message" do
in_session :visitor do
visit "/"
expect(page).to have_content "Welcome"
end
in_session :authorised_user do
sign_in(authorised_user)
visit "/"
expect(page).to have_content "Hello Joe Bloggs"
end
in_session :unauthorised_user do
sign_in(unauthorised_user)
visit "/"
expect(page).to have_content "Your subscription has expired"
end
end
module FeatureHelpers
def in_another_session(new_session = :other, &block)
old_session = Capybara.session_name
Capybara.session_name = new_session
driver = Capybara.current_session.driver
block.call(driver)
Capybara.session_name = old_session
end
alias_method :in_session, :in_another_session
end
RSpec.configure do |config|
config.include FeatureHelpers, type: :feature
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment