googleads
Testing Rails applications – Complete Ruby On Rails Testing Guide
preloder
Ruby Ruby on Rails

Testing Rails applications – Complete Ruby On Rails Testing Guide

This tutorial covers the basic setup and complete test cases of rails application. Basically, Testing helps to develop the behaviors of an application & TDD(Test Driven Development) is one of the major approach that used by most of the rails developers. Alongside, note to mind that in test-driven development (TDD), test is written first before writing the functional code & it is much simpler than you think.

Let’s go through the complete guide on testing Rails applications. To set up the Rails application for testing, you can use the below comment to create it.

Now you can change path to test-app directory and generate scaffold for the application,

 

Note: This will create following files under test/ directory

 

Now run the following command in application directory to create database and tables,

 

 

Test Rails Models

Rails Model test used to test all the model in our application. In rails, Models primary purpose is to interact database via ActiveRecord. We can also test CRUD operation of model object, validations and associations. Notably, Model test is way faster than the other test in rails. All the model test will be available under test/models/ directory.

Now open the test/models/article_test.rb file and add the below code,

Now run the test using the following command,

After passing this command, output screen will show you the number of test cases passed or failed.

 

Let’s focus on the failure test case now, as you can see we haven’t set the title and author as mandatory, but we expect to save this with title and author. It cannot be handled like this, so to make it mandatory you can add the following line to our app/models/article.rb file 

 

Now run the test so hopefully all the test cases will be passed this time.

 

 

Best To Read: How To Build Rock Solid Ruby On Rails App With BDD

 

Testing Rails Controllers

Rails Controller Test is entry point to every application. Controller handles the user request and it will return success/failure response based on the request type. While writing controller test, it will catch all the issues in controller, model, view and view helpers. We can also test particular template is rendered or not and you can test it by adding the following gem in to our Gemfile and bundle it.

 

Initially, while we creating scaffold, it will create the test/controllers/articles_controller_test.rb file by default. Now open this file and modify the index method as following

 

Now you can run the test using following command to test the index method

 

In  the above test case we have requested to list the articles and also we have requested to check whether the template is rendered or not.

 

 

Related:  Guide for Web Automation Testing With Ruby Cucumber And Watir

 

Rails System Test

Rails system test is similar to integration test, but it running tests in real browsers or headless drivers and will allow you to test user interaction of an application.

  • System test uses Capybara as base.
  • System test available under test/system directory.
  • By default, system tests are run with the Selenium driver, using the Chrome browser, and a screen size of 1400×1400.

Initially, while we create scaffold, it will also create the test/system/articles_test.rb file by default. Now you can open & uncomment the test/system/articles_test.rb file as following

 

And now run the test using below command,

It will open the chrome browser and will pass the test

 

If you want to run test in Firefox, then you can also change application_system_test_case.rb file like this

 

If you want to run the test without a opening browser then add the following gem into Gemfile and bundle it

 

Now modify  application_system_test_case.rb file like this

 

before running test make sure phantomjs is installed or not. If yes, then you can run it so will run the test without opening browser as we requested. By chance if you have not installed phantomjs then no need to worry! just use the below comment to install it.

 

Thats all guys! Now you can start running your test guys! Hope it helps! post us your favorite test case on comment section, would like to hear from you to serve you better.

Like What You're Reading ?

The following two tabs change content below.

Arjunan Subramani

Senior Software Engineer | Around 4 Years of experience in Web development and Testing arena. Plus, hands on expertise in Ruby, Ruby on Rails, Watir, Ruby Cucumber.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call