UserLimericksController#index
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.
UserLimericksController#index
displays only the limericks authored by one particular user.UserLimericksController#index
action.These differences change how the controller test must be written.
UserLimericksController#index
controller action.index
action.UserLimericksController#index
controller actionRecall that we must (1) find the route for this controller action and (2) determine if user authentication is required to access the action.
config/routes.rb
file, and locate the route that calls this index
action. You should find this route: get 'users/:user_id/limericks', to: 'user_limericks#index', as: 'user_limericks'
Note that, as per the as
part of this route, the URL helper will be named user_limericks_url
.
Note that the URI pattern ('users/:user_id/limericks'
) contains a parameter (:user_id
), so the URL helper will need to be passed an argument for that parameter (e.g., user_limericks_url(current_user)
).
Open the app/controllers/user_limericks_controller.rb
file in VS Code, and inspect the UserLimericksController
class defined within:
class UserLimericksController < ApplicationController
before_action :authenticate_user!
def index
@user = User.find(params[:user_id])
@limericks = @user.limericks.reverse_order
render :index
end
end
before_action
requires that the user be authenticated in order to access all the actions in this controller, including the index
action.index
actiontest/controller/user_limericks_controller_test.rb
in VS Code. Since we have yet to edit this file, it contains only the commented-out test case generated by default, "the truth"
.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
"should get index"
. test "should get index" do
assert true
end
Note that it is OK that this test case has the same name as the one we made earlier because the test cases are members of different controller test classes.
Arrange. Make the following changes to the test case to prepare the system to run the index
action.
User
fixture one
from the database. The limericks for which this user is the author will be the focus of this test case. Recall from the fixture code that this user authored two limericks.limericks_size
.sign_in
Devise helper, so the test will be able to execute the index
method without error. test "should get index" do
user = users(:one)
limericks_size = user.limericks.size
sign_in user
end
Note that the fixture methods, such as users
, can be used to retrieve an individual fixture by passing a symbol with the name of the fixture, such as :one
.
Note that we must capture the size of this user’s set of limericks (rather than all the limericks stored in the database) as only this user’s limericks will be displayed on the page.
Act. Use the get
command to simulate an HTTP GET request with the URL for the index page as follows.
test "should get index" do
user = users(:one)
limericks_size = user.limericks.size
sign_in user
get user_limericks_url(user)
end
assert true
call with calls to these two assertions:
assert_response
: checks that the HTTP response produced by the app has a success code (a 2xx status code, like 200 OK
).assert_select
: checks that the correct number of cards were rendered on the page. 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
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
UserLimericksControllerTest#test_should_get_index
is now included in the list of test cases that ran.