close

Hey guys,

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.

 

Setup

Lets say you have developed your app and are beginning with testing.

First, add Capybara to your Gemfile:

Screen Shot 2015-02-21 at 1.13.30 am

Run bundle install . Also run rails generate rspec:install if rspec hasn’t been installed.

In spec/spec_helper.rb, add  require calls for Capybara close to the top.

Screen Shot 2015-02-21 at 1.16.34 am

You’re all set to begin writing tests!

Capybara’s methods will be available in all files in the spec/requests and spec/integration directory.

 

Example #1

So, for example, in spec/integration/home_spec.rb you might write:

Screen Shot 2015-02-21 at 1.21.30 am

 

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.

Example #2

Lets take up a slightly more complex scenario. Say, we are testing the sign-up functionality:

Screen Shot 2015-02-21 at 1.32.10 am

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/requests or spec/integration directory. You can do this as per convenience.

 

Running Tests

To run tests execute the following command in terminal:

bundle exec rspec spec/requests or bundle exec rspec spec/integration

(Depending on the folder inside which you create your test file).

Capybara has a bunch of features to simulate all kinds of user interaction. You can find them here and here .

One aspect that Capybara falls short in is when JavaScript comes into the picture. But for that we have trusty old Selenium. More on that in the next article!

If you liked this article do join our mailing list. (Scroll up and click the green button on left side)

Adios!

Rushil Agrawal

The author Rushil Agrawal