Undefined `env 'method for nil: NilClass

I updated rspec from version 2 to 3. After that, I ran into this problem:

Failures:

  1) AlbumsController GET #edit 
     Failure/Error: sign_in_and_switch_schema @user
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/support/auth_helpers.rb:10:in `sign_in_and_switch_schema'
     # ./spec/controllers/albums_controller_spec.rb:12:in `block (2 levels) in <top (required)>'

spec_helper.rb contains:

 RSpec.configure do |config|
    # most omitted
    config.include Warden::Test::Helpers
    config.include Devise::TestHelpers, type: :controller
 end

albums_controller_spec.rb

describe AlbumsController do

  let(:album) { create(:album) }

  before(:all) do
    @user = create :user
  end

  before(:each) do
    sign_in_and_switch_schema @user
  end

  after(:all) do
    destroy_users_schema @user
    destroy_user @user
  end

 # describe part omitted
end

auth_helpers.rb in which the error occurred:

def sign_in_and_switch_schema(user)
 # binding.pry
 @request.env["devise.mapping"] = Devise.mappings[:user] # <- error line
 sign_in :user, user

 Apartment::Tenant.switch(user.username) 
end

I was looking for another simmilar Q&A but found nothing. Let me know if I should include something else. Thanks in advance.

+4
source share
2 answers

The solution was to add to spec_helper.rb:

RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
end
+7
source

According to the Devise TestHelper documentation , you should use

@request.env["devise.mapping"] = Devise.mappings[:user]

, Devise. , AlbumController Devise Controller. , .

, sign_in :

def simple_sign_in_and_switch(user)
  sign_in :user, user
  Apartment::Tenant.switch(user.username)
end

:

before(:each) do
  simple_sign_in_and_switch_schema @user
end

, , , - . , , .

+3

All Articles