Add a controller test for LimericksController#index

❮ Back Next ❯

Controller tests in Rails are automated tests in which we simulate HTTP requests to check whether our controllers process and respond to them correctly.

Tasks

Inspect the LimericksController#index controller action

There are a couple pieces of information that we must collect about the controller action we mean to test:

  1. Which URL helper can be used to retrieve a URL that will execute the action?
    • We will use this helper to simulate the HTTP request in our test.
  2. Is user authentication required to execute the action?
    • If authentication is required, we would need to set up an authenticated user in our test.

  root to: 'limericks#index'
class LimericksController < ApplicationController

  before_action :authenticate_user!, except: [:index]
  before_action :require_permission, except: [:index, :new, :create]

  def index
    @limericks = Limerick.all.reverse_order
    render :index
  end

  ...

Add a controller test for the index action

  # test "the truth" do
  #   assert true
  # end
  test "should get index" do
    assert true
  end
  test "should get index" do
    limericks_size = limericks.size
    assert true
  end
  test "should get index" do
    limericks_size = limericks.size
    get root_url
    assert true
  end
  test "should get index" do
    limericks_size = limericks.size
    get root_url
    assert_response :success
    assert_select 'div.card', limericks_size
  end

Run the controller test

rails test -v
Running 1 tests in a single process (parallelization threshold is 50)
Run options: -v --seed 29234

# Running:

LimericksControllerTest#test_should_get_index = 2.17 s = .

Finished in 2.178224s, 0.4591 runs/s, 0.9182 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips

❮ Back Next ❯