It is common in web application integration tests to require a signed in user. RSpec and Capybara tests for a Rails application could look like this:
visit root_path fill_in "Email", with: "firstname.lastname@example.org" fill_in "Password", with: "password" click_on "Sign In" # ...
This is redundant and makes the test suite slow if we write the same setup for every test that requires a signed in user.
If Clearance is being used for authentication, a back door can be inserted as Rack middleware. Instead of visiting, loading, and submitting the sign in form, it directly sets the designated user's ID into the session.
MyRailsApp::Application.configure do # ... config.middleware.use Clearance::BackDoor # ... end
Then, include a user in an
as parameter in integration tests:
visit dashboard_path(as: user)
It works for any URL:
visit new_feedback_path(as: giver)
The speed increase can be substantial. On one project using this technique, the test suite time was reduced 23%.