Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara, RSpec and Devise: any way to make integration tests faster by circumventing slow login and setting session directly?

For nearly every integration test, a user has to be signed into Devise. This takes a lot of time, so I wondered whether there's a way to set up the user session without having to visit the login page, enter details, and press the login button.

Maybe there's a helper method built into Devise that immediately signs a given user in?

Thanks a lot for help.

like image 769
Joshua Muheim Avatar asked Nov 01 '12 09:11

Joshua Muheim


1 Answers

In the header of your spec file, insert include Warden::Test::Helpers and Warden.test_mode!, like this:

require 'spec_helper'

include Warden::Test::Helpers
Warden.test_mode!

describe "AuthenticationPages" do

let(:user) { FactoryGirl.create(:user) }

before { login_as(user, scope: :user }
...

In above code, i used FactoryGirl to create an user. You can use other ways you like to create user. Then I login user by using method login_as . Then you can run any test and you can sure user has loged in. I think this is what you want, hope this help. You can see more details here Test with capybara.

EDIT

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)
like image 146
Thanh Avatar answered Sep 23 '22 07:09

Thanh