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.

pawel7318
  • 3,383
  • 2
  • 28
  • 44
  • 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? – Marc Lainez Aug 12 '14 at 18:42
  • sorry, I missed that one - Q updated – pawel7318 Aug 12 '14 at 19:29
  • @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. – pawel7318 Aug 12 '14 at 19:40

2 Answers2

7

Solution was to add to spec_helper.rb:

RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
end
pawel7318
  • 3,383
  • 2
  • 28
  • 44
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.

Marc Lainez
  • 3,070
  • 11
  • 16
  • 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. – pawel7318 Aug 13 '14 at 07:05
  • 1
    check out this thread, it might help you figure out what's happening => http://stackoverflow.com/questions/23859653/rails-devise-rspec-undefined-method-sign-in – Marc Lainez Aug 13 '14 at 07:08
  • I got the solution ! `config.infer_spec_type_from_file_location!`. Thank you for your time and effort anyway. – pawel7318 Aug 13 '14 at 07:09
  • The link you gave me shows another way to specify spec type. It's correct as well. – pawel7318 Aug 13 '14 at 07:37