Setting Up Rails System Tests

Learn to configure Capybara and our system specs in this lesson.

Rails system tests

Rails system tests, added in Rails 5.14, allow you to perform high-level, end-to-end tests of your application. Rather than testing if individual functions or methods perform as they should (unit testing), they test the application based on how the user interacts with it, that is, via a web interface. They allow us to assert that when a user interacts with our application in a certain way (such as filling in forms, clicking links or buttons), the app responds as we would expect (such as displaying the correct page or having the correct things appear on the page).

Although this kind of end-to-end test was possible previously, for example, with RSpec Feature specs, system tests bring a number of new benefits. We no longer have to worry about the cleanup of our database during the tests, which was commonly done using the Database Cleaner gem. Instead, system tests run the browser driver code in the same process as Rails, allowing the tests to be performed in transactions that are rolled back.

Though slower to run, end-to-end tests like these are arguably the most important type of tests for your app since they verify that the capabilities an app was created to provide actually work as expected. Even with 100 percent unit test coverage, one typo in a config file can stop the entire application from functioning correctly. The only way to know for sure is to load the app in a browser and actually use it.

Capybara

System specs rely on the Capybara gem in order to function. This provides a nice domain-specific language (DSL) for interacting with the browser. Following the instructions for Capybara, the first step is to install the gem.

Let’s add it to our Gemfile now:

Get hands-on with 1200+ tech skills courses.