0
0
Ruby on Railsframework~10 mins

Controller tests in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controller tests
Write test for controller action
Run test suite
Rails runs controller action
Controller processes request
Controller returns response
Test checks response and side effects
Test passes or fails
Fix code or test if needed
Repeat
Controller tests run your Rails controller actions and check their responses and effects step-by-step.
Execution Sample
Ruby on Rails
test "should get index" do
  get :index
  assert_response :success
  assert_not_nil assigns(:items)
end
This test calls the index action, checks for success response, and verifies assigned variables.
Execution Table
StepActionController StateResponseTest AssertionResult
1Call get :indexProcessing index actionPendingNone yetContinue
2Controller fetches itemsItems loaded into @itemsPendingNone yetContinue
3Controller renders index viewReady to respond200 OKassert_response :successPass
4Test checks assigns(:items)@items present200 OKassert_not_nil assigns(:items)Pass
5Test endsCompleted200 OKAll assertions passedTest Passed
💡 Test ends after all assertions pass or fail
Variable Tracker
VariableStartAfter Step 2After Step 3Final
@itemsnilArray of items loadedArray of items loadedArray of items loaded
Key Moments - 3 Insights
Why does the test check 'assigns(:items)' after calling get :index?
Because the controller sets @items in the index action, the test verifies it to ensure the controller prepared data for the view (see execution_table step 4).
What does 'assert_response :success' check in the test?
It checks that the controller responded with HTTP status 200 OK, meaning the request was handled successfully (see execution_table step 3).
Why do we call 'get :index' in the test?
Calling 'get :index' simulates a browser requesting the index page, triggering the controller action to run (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status after the controller renders the index view?
A200 OK
B404 Not Found
C500 Internal Server Error
D302 Redirect
💡 Hint
Check the Response column at Step 3 in the execution_table
At which step does the test verify that @items is not nil?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the Test Assertion column in the execution_table
If the controller did not assign @items, which test assertion would fail?
Aassert_response :success
Bget :index
Cassert_not_nil assigns(:items)
DTest ends
💡 Hint
Refer to the assertion checking assigns(:items) in execution_table step 4
Concept Snapshot
Controller tests in Rails:
- Use HTTP verbs like get, post to call actions
- Check response status with assert_response
- Verify instance variables with assigns(:var)
- Ensure controller processes requests and renders views correctly
- Run tests to catch errors early
Full Transcript
Controller tests in Rails simulate browser requests to controller actions. The test calls an action like index using get :index. The controller runs the action, loads data into instance variables like @items, and renders a view. The test then checks the HTTP response status to confirm success and verifies that expected variables are assigned. If all assertions pass, the test passes. This process helps ensure your controller behaves as expected before deploying your app.