Integration tests check if different parts of your Rails app work well together. They help find problems that unit tests might miss.
Integration tests in Ruby on Rails
require 'test_helper' class UserFlowsTest < ActionDispatch::IntegrationTest test "user can log in and see dashboard" do get login_path assert_response :success post login_path, params: { session: { email: 'user@example.com', password: 'password' } } follow_redirect! assert_select 'h1', 'Dashboard' end end
Integration tests use ActionDispatch::IntegrationTest in Rails.
You simulate user actions like visiting pages, submitting forms, and following redirects.
get root_path assert_response :success
post users_path, params: { user: { name: 'Alice', email: 'alice@example.com' } }
assert_redirected_to user_path(User.last)follow_redirect! assert_select 'p.notice', 'Welcome!'
This test simulates a user visiting the login page, submitting their email and password, and then seeing a welcome message on the redirected page.
require 'test_helper' class LoginFlowTest < ActionDispatch::IntegrationTest test "user logs in and sees welcome message" do get login_path assert_response :success post login_path, params: { session: { email: 'user@example.com', password: 'password' } } assert_response :redirect follow_redirect! assert_response :success assert_select 'h1', 'Welcome, user@example.com!' end end
Integration tests are slower than unit tests because they simulate real user actions.
Use fixtures or factories to set up test data before running integration tests.
Check HTML elements with assert_select to verify page content.
Integration tests check how parts of your app work together from a user's view.
They simulate visiting pages, submitting forms, and following redirects.
Use ActionDispatch::IntegrationTest and helper methods like get, post, and assert_select.
Practice
Solution
Step 1: Understand integration test scope
Integration tests simulate user actions and check multiple parts working together.Step 2: Compare options with integration test purpose
Only To check how different parts of the app work together from a user's perspective describes testing how parts work together from a user's view.Final Answer:
To check how different parts of the app work together from a user's perspective -> Option DQuick Check:
Integration tests = user perspective checks [OK]
- Confusing integration tests with unit tests
- Thinking integration tests only check views
- Assuming integration tests test database schema
Solution
Step 1: Identify the base class for integration tests
Rails integration tests inherit from ActionDispatch::IntegrationTest.Step 2: Check each option's base class
Only class UserFlowsTest < ActionDispatch::IntegrationTest inherits from ActionDispatch::IntegrationTest, others are for models or controllers.Final Answer:
class UserFlowsTest < ActionDispatch::IntegrationTest -> Option BQuick Check:
Integration test class inherits ActionDispatch::IntegrationTest [OK]
- Using ActiveRecord::Base which is for models
- Using ApplicationController which is for controllers
- Using ActionController::TestCase which is for controller tests
test "visit home page" do get root_path assert_response :success assert_select "h1", "Welcome" end
What does
assert_select "h1", "Welcome" check?Solution
Step 1: Understand assert_select usage
assert_select checks HTML elements and their content in the response body.Step 2: Analyze the parameters "h1" and "Welcome"
It looks for an <h1> tag with text exactly matching "Welcome".Final Answer:
That the page contains an <h1> tag with text exactly 'Welcome' -> Option AQuick Check:
assert_select checks HTML element and text [OK]
- Thinking assert_select only checks tag presence without text
- Confusing assert_response with assert_select
- Assuming assert_select checks redirects
test "create user" do
post users_path, params: { user: { name: "Alice" } }
assert_redirected_to user_path(@user)
endSolution
Step 1: Check variable usage in assertion
The test asserts redirect to user_path(@user), but @user is never set.Step 2: Understand how to get created user
After post, the created user should be fetched from controller or database, not assumed as @user.Final Answer:
Using @user without defining or assigning it in the test -> Option CQuick Check:
Undefined @user causes error in redirect assertion [OK]
- Assuming @user is auto-set after post
- Confusing HTTP verbs for create actions
- Incorrect params hash structure
Solution
Step 1: Identify correct HTTP verb for form submission
Login forms usually submit with POST, so use post with params.Step 2: Check correct assertion after login
After successful login, user should be redirected to dashboard, so assert_redirected_to dashboard_path is correct.Final Answer:
post login_path, params: { session: { email: "user@example.com", password: "secret" } } followed by assert_redirected_to dashboard_path -> Option AQuick Check:
POST login + assert redirect to dashboard [OK]
- Using get instead of post for form submission
- Checking response success instead of redirect
- Posting to dashboard_path instead of login_path
Ready to prove it?
Pass the test and this pattern moves unknown
