-
Notifications
You must be signed in to change notification settings - Fork 5.5k
How To: Test with Capybara
Acceptance tests for your application may require that a test user be logged in. To do this in your application you can either sign in the user using capybara by visiting the sign in url and entering valid credentials, or you can stub the logged in user.
Signing in via Capybara is perhaps the most 'correct' way since we want our tests to be as close to real world conditions as possible, however if you have more than a few tests, these few extra actions for each logged in test can be quite time consuming. The alternative is to use warden's built in stubbing actions to make your application think that a user is signed in but without all of the overhead of actually signing them in.
To do this, you'll need to include warden test helpers and turn on test mode
include Warden::Test::Helpers
Warden.test_mode!
Then you can make a call to the warden helper login_as
with a user resource and specifying the :scope => :user
to 'log in' a test user.
user = Factory.create(:user)
login_as(user, :scope => :user)
To make sure this works correctly you will need to reset warden after each test you can do this by calling
Warden.test_reset!
If for some reason you need to log out a logged in test user, you can use Warden's logout
helper.
logout(:user)
Now you should be good to go.
If you're wondering why we can't just use Devise's built in sign_in
and sign_out
methods, it's because these require direct access to the request object which is not available while using Capybara. To bundle the functionality of both methods together you can create a helper method.
If you have trouble using Warden's login_as method with the capybara-webkit driver, try setting run_callbacks to false in the login_as options struct
user = Factory.create(:user)
login_as(user, :scope => :user, :run_callbacks => false)
Good luck and happy testing. Blog post with more details