Capybara is an exellent choice for writing integration tests in your Rails apps. Rails doesn’t come with Capybara built in, but never worry, as setting up and using Capybara is reletively smooth. In this post, I intend to show you how to do exactly that. So, let’s get to it:
First add the following lines to your
and run the
Next, add the following to your
1 2 3 4 5
Capybara helps you test web applications by simulating how a real user would interact with your app. To achieve that, you need to mimick user actions such as visiting urls, clicking links/buttons, filling in form data, etc. And that’s exactly what Capybara DSL allows you to do (and more), with its extensive library. Find the Reference here.
config/environments/test.rb file, and add the following line to it:
Note that for this to work, you need to have qt library installed. on Mac it’s as simple as
brew install qt.
At this point we are done with the setup. To use Capybara, you first need to generate an integration test with the following rails command:
1 2 3
..or inside the test case itself:
1 2 3 4 5 6 7 8 9
Keep in mind that switching the driver creates a new session, so you may not be able to switch in the middle of a test.
And that’s about it! Capybara is a very simple and powerful tool for testing. I highly recommend you start using it.