undefined method `env' for nil:NilClass
Asked Answered
A

2

5

I upgraded rspec from version 2 to 3. After that I faced that 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's part omitted
end

auth_helpers.rb part on which 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 helped. Let me know if I should include something more. Thanks in advance.

Allina answered 12/8, 2014 at 17:10 Comment(3)
The error occurs in spec/support/auth_helpers.rb on line 10 in the sign_in_and_switch_schema method can you show the content of that file?Melanism
sorry, I missed that one - Q updatedAllina
@mlainez: I added binding.pry to the auth_helpers.rb. It shows clearly that @request is nil. I have no clue what else I can do with it but I can check if you tell me.Allina
A
7

Solution was to add to spec_helper.rb:

RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
end
Allina answered 13/8, 2014 at 7:28 Comment(0)
M
4

According to the Devise TestHelper documentation, you should only be using

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

when you are testing a controller that inherits a Devise controller. I guess the AlbumsController doesn't inherits from a Devise Controller. I don't think you need that line for these tests.

Try removing this line or creating another helper method that does only the sign_in and the switch:

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

And then call that method instead in your test case:

before(:each) do
  simple_sign_in_and_switch_schema @user
end

If this works try removing the faulty line completely and run your full test suite to see if it is needed somewhere else. If it is, extract that line in another method and use it only when you need.

Melanism answered 12/8, 2014 at 19:57 Comment(4)
Now it shows me NoMethodError: undefined method 'sign_in'. I think config.include Devise::TestHelpers, type: :controller doens't work with Rspec 3 anymore. I tried to remove type: :controller part but than it shows me big stack trace and didn't even went to pry part.Allina
check out this thread, it might help you figure out what's happening => #23860153Melanism
I got the solution ! config.infer_spec_type_from_file_location!. Thank you for your time and effort anyway.Allina
The link you gave me shows another way to specify spec type. It's correct as well.Allina

© 2022 - 2024 — McMap. All rights reserved.