Rails Functional Testing Controllers for Beginners - Part 2

Published: 2014-09-17
This article is continuing on from Part 1 and dives further into writing functional tests for your controllers.

Here are the common tests that I use when writing functional tests for my Rails controllers.

First make sure you have some form of automated testing setup as per my previous article. This will allow your tests to automatically run whenever you modify your test or model file. This will save a lot of time. If you have notifications setup, even better, you can just wait for the alert to pop up.

NOTE: The following versions were used for this setup:

  • Rails: 4.1.5
  • Ruby 2.1.2
  • MiniTest 5.4.0

Testing for Divs in Views

Generally you will probably have views that will be showing different content on the same page depending on what actions are taken. In this case its great to be able to check for the presence of unique ID's that should or should NOT be there.

The trick is to use unique ID tags for your divs in your views so you can easily check for them in your tests. Here is an example of the ID tag.

<div id="product_admin">
Admin Content goes here.
</div>
<div id="product">
Regular Content goes here.
</div>

This is also great for testing to make sure that admin content only appears to an admin and not a regular user.

Here is how to do the functional test:

test "should get show as regular user" do
  # this is based off devise, this is a regular user
  sign_in users(:regular) 
  get :index, product_id: 1
  assert_response :success
  # regular users can't see the admin panel
  assert_select "#product_admin", count: 0
  # regular users can see the admin panel
  assert_select "#product", count: 1
end
test "should get show as admin user" do
  # this is based off devise, this is an admin user.
  sign_in users(:admin) 
  get :index, company_id: 1
  assert_response :success
  # admin users can see both panels
  assert_select "#product_admin", count: 1
  assert_select "#product", count: 1
end
As you can see, this is a simple little test that can be very handy to verify the presence of certain elements on your page.

Simple AJAX Javascript testing

While javascript testing is a whole chapter by itself, there is a basic test that you can do to ensure that your javascript views are at least being sent to the browser without any errors.

test "update product via javascript" do
  # use xhr to simulate a ajax call
  xhr :get, :product_update, format: :js, product_id: '77'
  # if the controller responds with a javascript file the response will be a success
  assert_response :success
end
As you can see, an effective way to test that your .js views are responding correctly.

Testing the actual content of views

If you are going to do a refactor of the logic in your application views, then you can check the actual rendered content of views is still correct using assert_select.

Here is the view:

# app/views/products/show.html.erb
<div id="post_content">
  <p>
    <strong>Name:</strong>
    <%= @product.title %>
  </p>

  <p>
    <strong>Description:</strong>
    <%= @product.description %>
  </p>
</div>

Here is the test of the view contents:

test "should show content of product page correctly" do
  # do a standard get of the product
  get :show, id: products(:one)
  assert_response :success
  #check that the content matches
  assert_select '#product_content', html: '<p>
    <strong>Name:</strong>
    MyString
  </p>

  <p>
    <strong>Description:</strong>
    MyText
  </p>'
Now you can refactor your view/controller and be assured that the final output is unmodified from what it was originally.

Where to start

If you already have a Rails application working and don't have any tests, its easy to get started. The basic scaffold tests for your controllers are a great way to get started and you can slowly build up from there.

Your own tests

As you can see, it is easy to get started writing tests that you can rely on to ensure your application is working. You can slowly build up your test suite over time.

Hopefully this has helped you to get more confident with writing tests that you can rely on.

Resources

RailsGuides for Testing - The Rails Guides guide to testing applications.

Assert Select Documentation - For a deeper dive into assert select and what it can do.

Rails Automated Testing Setup for Beginners - The first article on how to setup automated testing in Rails.

Rails Unit Testing with MiniTest for Beginners - Getting started testing your Rails application with Unit testing using MiniTest. Also includes fixtures.

Rails Functional Testing Controllers for Beginners - Part 1 - The previous article on how to setup functional controller tests in Rails.



comments powered by Disqus