0
0
Ruby on Railsframework~20 mins

Integration tests in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Rails integration test?
Consider this integration test that simulates a user logging in and visiting the dashboard page. What will the test assert about the response?
Ruby on Rails
require "test_helper"

class UserFlowsTest < ActionDispatch::IntegrationTest
  test "login and visit dashboard" do
    post login_path, params: { session: { email: "user@example.com", password: "password" } }
    follow_redirect!
    get dashboard_path
    assert_response :success
    assert_select "h1", "Dashboard"
  end
end
AThe test will fail because follow_redirect! is called before get dashboard_path.
BThe test will fail because assert_select cannot be used in integration tests.
CThe test will raise an error because post login_path requires a CSRF token.
DThe test will pass if the dashboard page loads with an <h1> containing 'Dashboard'.
Attempts:
2 left
💡 Hint
Remember that follow_redirect! follows the redirect after login, and assert_select works in integration tests.
state_output
intermediate
2:00remaining
What is the value of session[:user_id] after this integration test runs?
In this integration test, a user logs in. What will be the value of session[:user_id] after the post request?
Ruby on Rails
post login_path, params: { session: { email: "user@example.com", password: "password" } }

# What is session[:user_id] now?
AIt will be the id of the logged-in user (an integer).
BIt will be nil because session is not accessible in integration tests.
CIt will be a string containing the user's email.
DIt will raise an error because session is private in integration tests.
Attempts:
2 left
💡 Hint
Think about how integration tests simulate requests and what is accessible.
🔧 Debug
advanced
2:00remaining
Why does this integration test fail with a 'No route matches' error?
This integration test tries to visit a profile page but fails with a routing error. What is the cause?
Ruby on Rails
get profile_path(user_id: 1)
assert_response :success
AThe routes file does not define a profile_path route.
BThe test forgot to log in the user before accessing the profile page.
CThe route helper profile_path requires a user object, not a user_id parameter.
DThe get method requires params to be passed as a separate hash, not inside the path helper.
Attempts:
2 left
💡 Hint
Check your config/routes.rb for the profile_path definition.
📝 Syntax
advanced
2:00remaining
Which option correctly writes an integration test to check a redirect after logout?
You want to test that after logout, the user is redirected to the home page. Which code snippet is correct?
A
delete logout_path
follow_redirect!
assert_response :success
assert_select "h1", "Home"
B
delete logout_path
assert_redirected_to root_path
C
post logout_path
assert_response :redirect
assert_redirected_to root_path
D
get logout_path
assert_response :success
assert_select "h1", "Home"
Attempts:
2 left
💡 Hint
Remember logout is usually a DELETE request and you must follow redirects to check final page content.
🧠 Conceptual
expert
3:00remaining
What is the main difference between Rails integration tests and controller tests regarding session and cookies?
Choose the statement that best describes how session and cookies behave differently in Rails integration tests compared to controller tests.
AIntegration tests require manual management of session and cookies, while controller tests automatically handle them.
BBoth integration and controller tests provide direct access to session and cookies after each request.
CIntegration tests simulate multiple requests and do not expose session or cookies directly, while controller tests allow direct access to session and cookies.
DController tests simulate full browser behavior including cookies, but integration tests do not.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of integration vs controller tests.