What if your app could check itself for bugs every time you save your code?
Why Controller tests in Ruby on Rails? - Purpose & Use Cases
Imagine you build a web app and manually click through every page and form to check if everything works after each change.
Manually testing controllers is slow, easy to forget, and you might miss bugs that break your app's flow.
Controller tests automatically check your app's responses and actions, catching errors early and saving time.
Visit page, fill form, submit, check result by eye
get :index; assert_response :success; assert_template :index
It lets you confidently change code knowing your app's behavior stays correct.
When adding a new feature, controller tests ensure users can still log in and see their dashboard without errors.
Manual testing is slow and unreliable.
Controller tests automate checking app responses.
This leads to faster, safer development.
Practice
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 AQuick Check:
Controller tests check web requests = A [OK]
- Confusing controller tests with database or view tests
- Thinking controller tests check CSS or JavaScript
- Assuming controller tests modify database schema
index action in a Rails controller test?Solution
Step 1: Recall Rails controller test syntax
In Rails controller tests, the correct way to simulate a GET request is usingget :action_name.Step 2: Analyze options
get :index usesget :index, which is correct syntax. Others are invalid or incorrect Ruby syntax.Final Answer:
get :index -> Option DQuick Check:
Use symbol with get method = C [OK]
- Using string or method call syntax instead of symbol
- Omitting colon before action name
- Using parentheses incorrectly
get :show, params: { id: 1 }
assert_response :success
assert_not_nil assigns(:user)What does
assert_not_nil assigns(:user) check?Solution
Step 1: Understand assigns method in controller tests
Theassigns(:user)method accesses the instance variable@userset by the controller.Step 2: Interpret assert_not_nil usage
assert_not_nil assigns(:user)checks that@useris not nil, meaning it was assigned.Final Answer:
That the @user variable was assigned in the controller -> Option AQuick Check:
assigns(:user) checks @user variable = B [OK]
- Thinking assigns checks database existence
- Confusing assigns with response content
- Assuming assigns checks redirects
post :create, user: { name: 'Alice' }
assert_redirected_to user_path(assigns(:user))Solution
Step 1: Check syntax for post request with parameters
In Rails 5+, parameters must be wrapped insideparams:keyword.Step 2: Analyze given code
The code usespost :create, user: { name: 'Alice' }missingparams:, causing syntax error.Final Answer:
Missing params: keyword before user hash in post request -> Option CQuick Check:
Use params: for parameters in post requests = A [OK]
- Omitting params: keyword in request methods
- Confusing HTTP verbs for actions
- Misusing assertion methods
Solution
Step 1: Check HTTP verb and parameters
Creating a user uses POST withparams: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 BQuick Check:
POST with params, redirect, and flash notice = D [OK]
- Using get instead of post for create action
- Forgetting params: keyword
- Checking wrong flash key or missing redirect assertion
Ready to prove it?
Pass the test and this pattern moves unknown
