Turn your manual testers into automation experts Request a DemoStart testRigor Free

Ruby On Rails Testing

Ruby On Rails Testing

What is Ruby On Rails?

Ruby on Rails, or Rails, or simply RoR, is an open-source framework written in Ruby language and widely used for server-side web application development. David Heinemeier Hansson created this framework in 2004. The primary objective behind developing this framework is to support developers in building a robust web page easily and quickly. RoR follows the MVC model architecture pattern. MVC means Model, View, and Controller, where Model holds the code for the application’s data structure, and View contains the UI part which the user interacts with. Finally, the Controller gets the scripts that interconnect both View and Model. Many companies like AirBnB, SlideShare, Bloomberg, and GitHub use the RoR platform.

When it comes to testing software developed with Ruby on Rails, these are the primary testing types:
  • Unit testing
  • Integration testing
  • System testing

Unit Testing

Unit testing generally means testing small units or functions in the source code. But in terms of Rails, unit testing is done for models. Users can create a rails project using the command -rails new application_name. This command creates the model, migration, controller, and views. A folder named “test” gets made in the root. Inside the test folder, there will be many subdirectories, including Modules. New test files will be added to the Modules folder when a new Module gets generated. Consider that we are creating a model named “article”. A corresponding unit test file called “test/unit/article_test.rb” gets generated once the model is created.

A sample test file looks like this:
require "test_helper"
class ArticleTest < ActiveSupport::TestCase
    test "should not save article without title" do
      article = Article.new
        assert_not article.save
    end
    test "should save article with title" do
        article = Article.new
        assert_not article.save, ""Saved the article with a title"
    end	
End

Integration Testing

Integration testing is responsible for the interaction between different application parts to work together, such as testing the interaction between the view and the controller. There are two different ways to perform integration testing via views or controllers.

  1. Controllers are tested using API calls. HTTP requests are sent to the application and responses are validated with assertions.
  2. Views are tested using Capybara, which can be used with BDD (Behavior Driven Development) approach to test the application.

For the API calls, Rails utilizes ActionDispatch::IntegrationTest to provide integration tests to Minitest, the standard library testing framework for Ruby.

An example of a controller integration test is like this:
require "test_helper"
class RegistrationFlowsTest < ActionDispatch::IntegrationTest
  test 'register as new user' do
    get '/join'
    assert_response :success

    assert_difference 'ActionMailer::Base.deliveries.size' do
    post_via_redirect '/join',
                      user: {
                        email: 'name@domain.com',
                        username: 'name',
                        password: 'password'
                      }
  end

  assert_equal '/login', path
  assert_response :success
  end
end
For testing views, we can use two methods:
  1. Capybara and Rspec
  2. testRigor
  3. You can use the same tools for system testing, which is why we will discuss them in the next section.

System Testing

In system testing, we test user interactions with the application. Here tests are based on a user role. Scenarios are created based on the goal of covering every model, controller, and view.

For system testing, we can choose any of the tools below:
  1. Capybara and Rspec
  2. Cypress
  3. Selenium
  4. testRigor

Capybara and Rspec

Capybara is an acceptance testing framework often used for integration and end-to-end testing. Capybara can execute the automation in the headless browser option, which makes the execution faster. RSpec helps achieve the test scenarios for end-to-end testing. The metadata functionality of RSpec allows separate system test performance from other tests.

Here is a sample Capybara script:
# spec/features/visitor_signs_up_spec.rb
require 'spec_helper'

feature 'Visitor signs up' do
  scenario 'with valid email and password' do
    sign_up_with 'valid@example.com', 'password'

    expect(page).to have_content('Sign out')
  end

  scenario 'with invalid email' do
    sign_up_with 'invalid_email', 'password'

    expect(page).to have_content('Sign in')
  end

  scenario 'with blank password' do
    sign_up_with 'valid@example.com', ''

    expect(page).to have_content('Sign in')
  end

  def sign_up_with(email, password)
    visit sign_up_path
    fill_in 'Email', with: email
    fill_in 'Password', with: password
    click_button 'Sign up'
  end
end

Cypress

Cypress is a front end testing tool based on Javascript, which you can use with Chai as an assertion library and Mocha as the framework. It results in robust tests, which are fast to execute in the browser.

A sample Cypress script looks like this:
describe("User can", () => {
  beforeEach(() => {
    cy.deleteAndSeedDatabase();
  });

  afterEach(() => {
    cy.deleteDatabase();
  });

  it("visits home page and see list of places", () => {
    cy.visit("localhost:3000/places");
    cy.get("table").should("contain", "Mount Rushmore");
    cy.get("table").should("contain", "Hiroshima");
    cy.get("table").should("contain", "Dubai");
  });
});

Selenium

Selenium, a comprehensive open-source testing tool, doesn't provide standalone support to Rails applications. We need to install multiple components to make it work. You can get more details here.

testRigor

testRigor is an advanced no-code automation tool explicitly created for functional end-to-end testing. It provides a cloud-hosted infrastructure, allowing you to design and run tests in virtually any OS and browser. It also supports the BDD approach out of the box.

With testRigor:
  • Tests can be run across multiple browsers or devices simultaneously, saving execution time.
  • You can track any UI changes with visual testing.
  • Perform email validations (check deliverability, test attachments, open links)
  • testRigor provides 2FA testing support for Gmail, text messages, and Google authenticator.

No reliance on implementation details results in robust tests that simulate an entire flow of an end-user interacting with the application. The integrated AI helps testRigor capture elements by mentioning the text in the component or its position hierarchy. So any change in element hierarchy on a webpage won't make the script fail, thus eliminating false negative cases.

Below is an example of a test case for new user registration:
click "Sign up"
generate unique email and enter it into "Email", then save it as "generatedEmail"
generate unique name and enter it into "Name", then save it as "generatedName"
enter "PasswordSecure" into "Password"
click "Submit"
check that email to stored value "generatedEmail" was delivered
click "Confirm email"
check that page contains "Email was confirmed"
check that page contains expression "Hello, ${generatedName}"

As you can see in this example, the entire test is abstract from implementation details. The same goes for any other tests in testRigor, including tables (where you can specify elements on the UI layer just as a real user would).

Conclusion

Using the tools we've just discussed will help you form a solid testing pyramid, with various types of tests on every layer. Such automated tests are crucial for faster time-to-market and drastically lower defect escape rate.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.