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:
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
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>'
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.
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.
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.