Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save boddhisattva/f14d2e860dcd2d45f805eb698b99605f to your computer and use it in GitHub Desktop.
Save boddhisattva/f14d2e860dcd2d45f805eb698b99605f to your computer and use it in GitHub Desktop.
Setup System tests to work with RSpec, Devise and Rails 6

Setting this up took quite a bit of time and research for me, so just thought of sharing the learnings along the way that led to a working setup.

  • Initial error that I was getting with running System tests in Rails 6

    System test integration requires Rails >= 5.1 and has a hard dependency on a webserver and `capybara`, please add capybara to your Gemfile and configure a webserver (e.g. `Capybara.server = :webrick`) before attempting to use system tests.
    • since the error above says specify Capybara in the Gemfile, a part of my Gemfile looked like below:

      group :development, :test do
        gem 'capybara', '~> 3.13'
        gem 'factory_bot_rails'
        gem 'pry-byebug'
        gem 'rspec-rails'
        gem 'shoulda-matchers'
        gem 'webdrivers'
      end
  • What didn't work for me:

    • From an official wiki guide of testing sign in of a user with Devise for request type specs:
      • Context: Although the title says it's the guide for request type specs but since content of the wiki also said This is the equivalent of declaring include Devise::Test::IntegrationHelpers in an system/feature spec or Rails system/controller test. , I was trying this out with system specs & it didn't work for me
        • Why this guide didn't work fully for me?
          • It specifies the usage of tests of type request with this line of code: config.include Devise::Test::IntegrationHelpers, type: :request but that throws an error for me which says undefined method sign_in(please read below for more context on what the sign_in method is for) when running the required system spec. My exact setup: Rails 6.0.3.rc1, Ruby: 2.6.0, RSpec 3.9, Capybara 3.32.1
  • Solution: Steps that properly setup System tests for me in Rails 6 using RSpec & Devise :

    • For the sign_in (if you are using user sign in using Devise) method you need to implement the below steps to get up and running with system tests in Rails 6
      • other general code required to make things for system tests with Rails 6
        • Step 1: Specify gem 'webdrivers' in your Gemfile(already included as shown above) Why?

          • It's required for System tests to work with Rails 6
          • More details here
        • Step 2: Specify the required driver for Capybara. Why?

          • This is required for System tests to work with Rails 6

          • required code to be placed in the RSpec.configure do |config| block of your rails_helper.rb file:

              config.before(:each, type: :system) do
                driven_by :selenium_chrome_headless
              end
          • Please note: this is just one of the drivers that can be used here, there's a possibility that this would also work with other Capybara drivers also

        • Step 3: Specify the below helpers in the RSpec.configure do |config| block of your rails_helper.rb to make sure that the sign_in method is used to login the user into your app before testing the specific feature of your app which requires a signed in user

          config.include Devise::Test::IntegrationHelpers, type: :system
          config.include Warden::Test::Helpers
  • Reference credits:

@davidteren
Copy link

Thanks for this. Was helpful.

@meg-gutshall
Copy link

This is great info! You should send in a revision to the Devise Wiki article you linked. A lot of their articles need an update.

@maricavor
Copy link

How to configure session_store if chrome is running in docker. Sign_in method not logging in user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment