LimericksController#index
Controller tests in Rails are automated tests in which we simulate HTTP requests to check whether our controllers process and respond to them correctly.
LimericksController#index
controller action.index
action.LimericksController#index
controller actionThere are a couple pieces of information that we must collect about the controller action we mean to test:
config/routes.rb
file, and locate the route that calls this index
action. You should find this route: root to: 'limericks#index'
Note that this is the root route for the app. The URL helper for the root route is root_url
.
Open the controller class file app/controllers/limericks_controller.rb
, which contains the LimericksController
class, in VS Code. Inspect the code related to the index
method:
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
...
Note that (as per the before_action
declarations) no user authentication is required to run the index
action.
Note that the index
action behaves in the usual way, retrieving a collection of limericks from the database and rendering a view template app/views/limericks/index.html.erb
.
index
actiontest/controller/limericks_controller_test.rb
in VS Code. It should contain only one test case, "the truth"
test case that was generated by default and is commented out. # test "the truth" do
# assert true
# end
"should get index"
to communicate the controller action under test (index
) and the expected result (“should get”). test "should get index" do
assert true
end
Limerick
fixtures in a variable, so in the Assert step, we can check that the correct number of limerick cards are rendered on the index page. test "should get index" do
limericks_size = limericks.size
assert true
end
limericks
retrieves all the Limerick
fixture objects from the database.
get
command to simulate an HTTP GET request with the URL for the root route as follows. test "should get index" do
limericks_size = limericks.size
get root_url
assert true
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
limericks_size = limericks.size
get root_url
assert_response :success
assert_select 'div.card', limericks_size
end
assert_select
counts the number of HTML div
elements with CSS class card
and checks that the count is equal to limericks_size
(the number of Limerick
fixtures).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
LimericksControllerTest#test_should_get_index
) and that test had 2 assertions, which makes sense because we called assert_response
and assert_select
.