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.
- 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.
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.
- Controllers are tested using API calls. HTTP requests are sent to the application and responses are validated with assertions.
- 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.
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: '[email protected]', username: 'name', password: 'password' } end assert_equal '/login', path assert_response :success end end
- Capybara and Rspec
- testRigor
- 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.
- Capybara and Rspec
- Cypress
- Selenium
- 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.
# 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 '[email protected]', '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 '[email protected]', '' 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.
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.
- 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.
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).
How to do End-to-end Testing with testRigor
Let us take the example of an e-commerce website that sells plants and other gardening needs. We will create end-to-end test cases in testRigor using plain English test steps.
Step 1: Log in to your testRigor app with your credentials.
Step 2: Set up the test suite for the website testing by providing the information below:
- Test Suite Name: Provide a relevant and self-explanatory name.
- Type of testing: Select from the following options: Desktop Web Testing, Mobile Web Testing, Native and Hybrid Mobile, based on your test requirements.
- URL to run test on: Provide the application URL that you want to test.
- Testing credentials for your web/mobile app to test functionality which requires user to login: You can provide the app’s user login credentials here and need not write them separately in the test steps then. The login functionality will be taken care of automatically using the keyword
login
. - OS and Browser: Choose the OS Browser combination on which you want to run the test cases.
- Number of test cases to generate using AI: If you wish, you can choose to generate test cases based on the App Description text, which works on generative AI.
Step 3: Click Create Test Suite.
On the next screen, you can let AI generate the test case based on the App Description you provided during the Test Suite creation. However, for now, select do not generate any test, since we will write the test steps ourselves.
Step 4: To create a new custom test case yourself, click Add Custom Test Case.
Step 5: Provide the test case Description and start adding the test steps.
For the application under test, i.e., e-commerce website, we will perform below test steps:
- Search for a product
- Add it to the cart
- Verify that the product is present in the cart
Test Case: Search and Add to Cart
Step 1: We will add test steps on the test case editor screen one by one.
testRigor automatically navigates to the website URL you provided during the Test Suite creation. There is no need to use any separate function for it. Here is the website homepage, which we intend to test.
First, we want to search for a product in the search box. Unlike traditional testing tools, you can identify the UI element using the text you see on the screen. You need not use any CSS/XPath identifiers.
click "What are you looking for?"
Step 2: Once the cursor is in the search box, we will type the product name (lily), and press enter to start the search.
type "lily" enter enter
Search lists all products with the “lily” keyword on the webpage.
Step 3: The lily plant we are searching for needs the screen to be scrolled; for that testRigor provides a command. Scroll down until the product is present on the screen:
scroll down until page contains "Zephyranthes Lily, Rain Lily (Red)"
When the product is found on the screen, testRigor stops scrolling.
Step 4: Click on the product name to view the details:
click "Zephyranthes Lily, Rain Lily (Red)"
After the click, the product details are displayed on the screen as below, with the default Quantity as 1.
Step 5: Lets say, we want to change the Quantity to 3, so here we use the testRigor command to select from a list.
select "3" from "Quantity"
click "Add to cart"
The product is successfully added to the cart, and the “Added to your cart:” message is displayed on webpage.
Step 6: To assert that the message is successfully displayed, use a simple assertion command as below:
check that page contains "Added to your cart:"
Step 7: After this check, we will view the contents of the cart by clicking View cart as below:
click "View cart"
Step 8: Now we will again check that the product is present in the cart, under heading “Your cart” using the below assertion. With testRigor, it is really easy to specify the location of an element on the screen.
check that page contains "Zephyranthes Lily, Rain Lily (Red)" under "Your cart"
Complete Test Case
Here is how the complete test case will look in the testRigor app. The test steps are simple in plain English, enabling everyone in your team to write and execute them.
Click Add and Run.
Execution Results
Once the test is executed, you can view the execution details, such as execution status, time spent in execution, screenshots, error messages, logs, video recordings of the test execution, etc. In case of any failure, there are logs and error text that are available easily in a few clicks.
You can also download the complete execution with steps and screenshots in PDF or Word format through the View Execution option.
testRigor’s Capabilities
Apart from the simplistic test case design and execution, there are some advanced features that help you test your application using simple English commands.
- Reusable Rules (Subroutines): You can easily create functions for the test steps that you use repeatedly. You can use the Reusable Rules to create such functions and call them in test cases by simply writing their names. See the example of Reusable Rules.
- Global Variables and Data Sets: You can import data from external files or create your own global variables and data sets in testRigor to use them in data-driven testing.
- 2FA, QR Code, and Captcha Resolution: testRigor easily manages the 2FA, QR Code, and Captcha resolution through its simple English commands.
- Email, Phone Call, and SMS Testing: Use simple English commands to test the email, phone calls, and SMS. These commands are useful for validating 2FA scenarios, with OTPs and authentication codes being sent to email, phone calls, or via phone text.
- File Upload/ Download Testing: Execute the test steps involving file download or file upload without the requirement of any third-party software. You can also validate the contents of the files using testRigor’s simple English commands.
- Database Testing: Execute database queries and validate the results fetched.
testRigor enables you to test web, mobile (hybrid, native), API, and desktop apps with minimum effort and maintenance.
Additional Resources
- Access testRigor documentation to know about more useful capabilities
- Top testRigor’s features
- How to perform end-to-end testing
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.
Achieve More Than 90% Test Automation | |
Step by Step Walkthroughs and Help | |
14 Day Free Trial, Cancel Anytime |