Add a controller test for UserLimericksController#index

❮ Back Next ❯

In this part, we will make a controller test for the UserLimericksController#index controller action, which has some key differences from the LimericksController#index action we wrote a test for earlier.

These differences change how the controller test must be written.

Tasks

Inspect the UserLimericksController#index controller action

Recall that we must (1) find the route for this controller action and (2) determine if user authentication is required to access the action.

  get 'users/:user_id/limericks', to: 'user_limericks#index', as: 'user_limericks'
class UserLimericksController < ApplicationController

  before_action :authenticate_user!

  def index
    @user = User.find(params[:user_id])
    @limericks = @user.limericks.reverse_order
    render :index
  end

end

Add a controller test for the index action

require "test_helper"

class UserLimericksControllerTest < ActionDispatch::IntegrationTest
  # test "the truth" do
  #   assert true
  # end
end
class UserLimericksControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  # test "the truth" do
  #   assert true
  # end
end
  test "should get index" do
    assert true
  end
  test "should get index" do
    user = users(:one)
    limericks_size = user.limericks.size
    sign_in user
  end
  test "should get index" do
    user = users(:one)
    limericks_size = user.limericks.size
    sign_in user
    get user_limericks_url(user)
  end
  test "should get index" do
    user = users(:one)
    limericks_size = user.limericks.size
    sign_in user
    get user_limericks_url(user)
    assert_response :success
    assert_select 'div.card', limericks_size
  end

Run the controller test

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

# Running:

LimericksControllerTest#test_should_get_index = 2.37 s = .
UserLimericksControllerTest#test_should_get_index = 0.02 s = .

Finished in 2.398942s, 0.8337 runs/s, 1.6674 assertions/s.
2 runs, 4 assertions, 0 failures, 0 errors, 0 skips

❮ Back Next ❯