Login_as with rspec crash first test

Following the instructions of this Devise How-To page I am trying to rebuild my entire rspec test to bypass the user input process.

You can use 2 methods for this:

  • sign_in from Devise - which cannot be used with functional tests (Capybara)
  • login_as from Warden (Developer built on top of it).

The first method worked during the first shot (all test passes), except for those that Capybara had, so I decided to leave it for now.

2nd gives me some strange results - everything passes except for the first (any that I put as the first in the file). This fails when I run only one of them. I checked it with binding.pry and it fails because the user has not logged in and redirects it to the login page. Somehow the first test launches something that does the rest. I have no idea what is going on here.

I used around hook before, but it behaves really strange , so I changed it to a set of before and after (at the same time it works much faster since it creates only one user on begging, and not on every test). Here's what it looks like now:

 require 'spec_helper' describe AlbumsController do let(:album) { create(:album) } before(:all) do @user = create :user end before(:each) do login_and_switch_schema @user end after(:all) do destroy_users_schema @user destroy_user @user end describe "GET #new" do before { get :new } it { expect(response).to render_template :new } end describe "GET #edit" do before { get :edit, id: album } it { expect(response).to render_template :edit } end ... 

and I determined that helpers:

 Warden.test_mode! def login_and_switch_schema(user) #@request.env["devise.mapping"] = Devise.mappings[:user] #sign_in :user, user login_as(user, scope: :user) Apartment::Database.switch(user.username) end def destroy_users_schema(user) Apartment::Database.drop(user.username) Apartment::Database.reset end def destroy_user(user) User.destroy(user) end 

I would like to ask you for help.

+1
ruby-on-rails rspec devise
source share
1 answer

I would try moving the code before(:all) and after(:all) to before(:each) and after(:each) . :all does not work well with let , DatabaseCleaner or gives you a predictable execution order for the first test run.

+1
source share

All Articles