This is #2 in a series of articles about Testing in RoR. The first article gave a general overview about RoR and the testing methodologies involved.
The Focus for today will be on Integration Testing. I will first explain what Integration Tests are. Next a step-by-step, beginner friendly setup guide.
What Integration Testing is
Lets start with an example: Say I want to test whether the sign-up functionality of my app works. Such a test would involve visiting the sign-up page, filling up the form, clicking on the ‘Sign-Up’ button and checking if one is redirected to the Dashboard.
ie: I will try to simulate a real user interacting with the app.
This is exactly what Integration Testing is for. These tests emulate real-world scenarios. The app is treated as a ‘black-box’. Various functionalities are tested from the perspective of a user.
The only catch with all of this is that even a relatively small app has a large number of use-cases. Manually testing each one of them introduces the possibility of human-error. Not to mention the tedious nature of the work.
This is where the gems(pre-built libraries) of RoR come into play. There are a plethora of gems available which automate the entire process.
I will focus on two tried and tested gems which are the industry standard: Capybara and Rspec.
Lets say you have developed your app and are beginning with testing.
First, add Capybara to your Gemfile:
bundle install . Also run
rails generate rspec:install if rspec hasn’t been installed.
require calls for Capybara close to the top.
You’re all set to begin writing tests!
Capybara’s methods will be available in all files in the
So, for example, in
spec/integration/home_spec.rb you might write:
Within the describe block one can have many scenarios, each beginning with the it keyword. The block of text within quotes after the describe and it keywords is what will be displayed if the test fails. They can be set as per convenience. They need not be in any strict or rigid format.
Lets see what exactly we are doing inside this test:
1. We visit the root directory.
2. We expect the page to have content: ‘Welcome’.
As you may notice, all of capybara’s keywords are designed to be human readable.
Lets take up a slightly more complex scenario. Say, we are testing the sign-up functionality:
1.blog_posts(…) is method which returns the path of the desired page
2. the fill_in method, as the name suggests, fills in the element with id=’Author’ with text: ‘J. Random Hacker’
3. click_on finds the button or link with text (value for button) ‘Submit’ and clicks it.
4. We check whether ‘has been submitted’ is present on the page to verify if the button click has worked.
Thats how easy it is to write an integration test. More tests can be added as either ‘it’ blocks inside a describe block, or as a new describe block or even a new file in the
spec/integration directory. You can do this as per convenience.
To run tests execute the following command in terminal:
bundle exec rspec
spec/requests or bundle exec rspec
(Depending on the folder inside which you create your test file).
If you liked this article do join our mailing list. (Scroll up and click the green button on left side)