简体   繁体   中英

How to make configuration changes in RSpec/Capybara tests?

I am trying to write a feature spec using RSpec and Capybara which changes between two types of session_stores: cookie_store and active_record_store, and expects the user to remain logged in even though we switched from active_record_store to cookie_store. Basically for some reasons, I am switching from active_record_store to cookie_store and want to see if the session remains stored regardless of change in configuration.

This is my session_store.rb file:

# Be sure to restart your server when you modify this file.

Project::Application.config.session_store :cookie_store, :key => '_project_session'

This is my spec:

 describe "Admin logged into" do
    it 'shows the correct interface for logged in users' do
      # ---> Here use active_record_store:
      # Project::Application.config.session_store :active_record_store
      log_in_admin(admin)
      visit candy_shop_path
      expect(page.current_path).to eq(candy_shop_path)
 

      # --> Here switch to cookie_store
      # Project::Application.config.session_store :cookie_store, :key => '_project_session'
      visit candy_shop_path
      expect(page.current_path).to eq(candy_shop_path)
    end
  end

I need help with the commented out part. I am not sure how to configure RSpec to use:active_record_store first and then switch to:cookie_store.

Capybara doesn't really support this since, as you can see from the comment in session_store.rb, the app really needs to be restarted for any changes to take effect. You MIGHT be able to get something like this to work by having session_store.rb set it's value based on an environment variable and then manipulating the Capybara::Server instance at page.server to get it to restart the application under test but it would be a severe hack and I really wouldn't recommend it for testing purposes.

Note: You should never be doing expect(page.current_path).to eq(candy_shop_path) . It will lead to flaky tests and frustration. Basically you should never be using the standard RSpec matchers (eq, etc) when dealing with Capybara objects, instead use the Capybara provided matchers, which in this case would be expect(page).to have_current_path(candy_shop_path) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM