📖 Scenario: You are building a simple blog application in Rails. You want to make sure your PostsController works correctly by writing tests for its main actions.
🎯 Goal: Write controller tests for the PostsController to check that the index and show actions respond successfully and render the correct templates.
📋 What You'll Learn
Create a test file for PostsController in test/controllers/posts_controller_test.rb
Write a test for the index action that checks for a successful HTTP response
Write a test for the show action that checks for a successful HTTP response
Use Rails built-in test framework syntax
💡 Why This Matters
🌍 Real World
Controller tests help ensure that your web app's pages load correctly and respond as expected before you deploy your app.
💼 Career
Writing controller tests is a common task for Rails developers to maintain code quality and prevent bugs in web applications.
Progress0 / 4 steps
1
Setup test data
Create a Post record in the test database with title set to 'Test Post' and body set to 'This is a test post.'. Write this inside the setup method of PostsControllerTest.
Ruby on Rails
Hint
Use Post.create inside the setup method to create the test post.
2
Test the index action response
Add a test method called test_index_should_get_success that sends a get request to posts_url and asserts the response is successful using assert_response :success.
Ruby on Rails
Hint
Use get posts_url and then assert_response :success inside the test method.
3
Test the show action response
Add a test method called test_show_should_get_success that sends a get request to post_url(@post) and asserts the response is successful using assert_response :success.
Ruby on Rails
Hint
Use get post_url(@post) and assert_response :success inside the test method.
4
Complete the test file
Ensure the test file test/controllers/posts_controller_test.rb includes the setup method and both test methods test_index_should_get_success and test_show_should_get_success inside the PostsControllerTest class.
Ruby on Rails
Hint
Make sure the class and methods are properly closed with end.
Practice
(1/5)
1. What is the main purpose of a controller test in Rails?
easy
A. To check if web requests respond correctly and perform expected actions
B. To test database schema changes
C. To verify CSS styles are applied properly
D. To check JavaScript functions run without errors
Solution
Step 1: Understand controller test role
Controller tests simulate web requests to check if the controller responds correctly.
Step 2: Compare options
Only To check if web requests respond correctly and perform expected actions describes checking web requests and expected actions, which is the controller test's purpose.
Final Answer:
To check if web requests respond correctly and perform expected actions -> Option A
Quick Check:
Controller tests check web requests = A [OK]
Hint: Controller tests simulate web requests to check responses [OK]
Common Mistakes:
Confusing controller tests with database or view tests
Thinking controller tests check CSS or JavaScript
Assuming controller tests modify database schema
2. Which of the following is the correct syntax to simulate a GET request to the index action in a Rails controller test?
easy
A. get('/index')
B. get index
C. get index()
D. get :index
Solution
Step 1: Recall Rails controller test syntax
In Rails controller tests, the correct way to simulate a GET request is using get :action_name.
Step 2: Analyze options
get :index uses get :index, which is correct syntax. Others are invalid or incorrect Ruby syntax.
Final Answer:
get :index -> Option D
Quick Check:
Use symbol with get method = C [OK]
Hint: Use get :action_name with a symbol for controller tests [OK]
Common Mistakes:
Using string or method call syntax instead of symbol
4. Identify the error in this Rails controller test code:
post :create, user: { name: 'Alice' }
assert_redirected_to user_path(assigns(:user))
medium
A. assert_redirected_to should be assert_response
B. Using post instead of get for create action
C. Missing params: keyword before user hash in post request
D. assigns(:user) cannot be used in controller tests
Solution
Step 1: Check syntax for post request with parameters
In Rails 5+, parameters must be wrapped inside params: keyword.
Step 2: Analyze given code
The code uses post :create, user: { name: 'Alice' } missing params:, causing syntax error.
Final Answer:
Missing params: keyword before user hash in post request -> Option C
Quick Check:
Use params: for parameters in post requests = A [OK]
Hint: Always use params: keyword for request parameters [OK]
Common Mistakes:
Omitting params: keyword in request methods
Confusing HTTP verbs for actions
Misusing assertion methods
5. You want to test that after creating a new user, the controller redirects to the user's show page and sets a flash notice. Which test code correctly checks both behaviors?
hard
A. post :create, params: { user: { name: 'Bob' } }
assert_response :redirect
assert_nil flash[:notice]
B. post :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert_equal 'User created!', flash[:notice]
C. get :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert flash[:alert] == 'User created!'
D. post :create, user: { name: 'Bob' }
assert_response :success
assert flash[:notice] == 'User created!'
Solution
Step 1: Check HTTP verb and parameters
Creating a user uses POST with params: keyword, so post :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert_equal 'User created!', flash[:notice] is correct here.
Step 2: Verify assertions for redirect and flash
post :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert_equal 'User created!', flash[:notice] asserts redirect to user's show page and flash notice equals 'User created!'. This matches the requirement.
Step 3: Eliminate incorrect options
post :create, user: { name: 'Bob' }
assert_response :success
assert flash[:notice] == 'User created!' misses params: keyword and uses assert_response :success instead of redirect. get :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert flash[:alert] == 'User created!' uses GET instead of POST and wrong flash key. post :create, params: { user: { name: 'Bob' } }
assert_response :redirect
assert_nil flash[:notice] asserts flash[:notice] is nil, which is wrong.
Final Answer:
post :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert_equal 'User created!', flash[:notice] -> Option B
Quick Check:
POST with params, redirect, and flash notice = D [OK]
Hint: Use post with params:, assert_redirected_to, and assert_equal flash [OK]
Common Mistakes:
Using get instead of post for create action
Forgetting params: keyword
Checking wrong flash key or missing redirect assertion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.