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.
$ rails new test-app
Now you can change path to test-app directory and generate scaffold for the application,
$ cd test-app/ $ rails generate scaffold article title:string author:string body:text published:boolean published_at:datetime
Note: This will create following files under test/ directory
..... ..... invoke test_unit create test/models/article_test.rb create test/fixtures/articles.yml ....... create test/controllers/articles_controller_test.rb ....... create test/system/articles_test.rb .......
Now run the following command in application directory to create database and tables,
$ rake db:create $ rake db:migrate
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,
require 'test_helper' class ArticleTest < ActiveSupport::TestCase test 'should not save article without author and title' do article = Article.new assert_not article.save, 'Article saved without title and author' end test 'should save article' do article = Article.new(title: 'Rails - Complete Reference', author: 'Jhon') assert article.save, 'Article not saved' end end
Now run the test using the following command,
$ rake test TEST=test/models/article_test.rb
After passing this command, output screen will show you the number of test cases passed or failed.
Run options: --seed 42660 # Running: F Failure: ArticleTest#test_should_not_save_article_without_author_and_title [/home/arjun/test-app/test/models/article_test.rb:6]: Article saved without title and author bin/rails test test/models/article_test.rb:4 . Finished in 0.074699s, 26.7740 runs/s, 26.7740 assertions/s. 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
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
class Article < ApplicationRecord validates :title, :author, presence: true end
Now run the test so hopefully all the test cases will be passed this time.
Run options: --seed 37055 # Running: .. Finished in 0.089363s, 22.3806 runs/s, 22.3806 assertions/s. 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
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
test "should get index" do get articles_url assert_template :index assert_response :success end
Now you can run the test using following command to test the index method
$ ruby -I test test/controllers/articles_controller_test.rb -n test_should_get_index
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.
Run options: -n test_should_get_index --seed 27225 # Running: . Finished in 0.355492s, 2.8130 runs/s, 5.6260 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
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
test "visiting the index" do visit articles_url assert_selector "h1", text: "Article" end
And now run the test using below command,
$ rake test TEST=test/system/articles_test.rb
It will open the chrome browser and will pass the test
Run options: --seed 60599 # Running: Puma starting in single mode... * Version 3.12.0 (ruby 2.4.1-p111), codename: Llamas in Pajamas * Min threads: 0, max threads: 1 * Environment: test * Listening on tcp://0.0.0.0:33490 Use Ctrl-C to stop . Finished in 17.518490s, 0.0571 runs/s, 0.0571 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
If you want to run test in Firefox, then you can also change application_system_test_case.rb file like this
require "test_helper" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :firefox, screen_size: [1400, 1400] end
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
require "test_helper" require "capybara/poltergeist" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :poltergeist end
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.
$ sudo apt-get install phantomjs
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.
[contact-form-7 404 "Not Found"]