Rails 4 + Rspec + Devise: undefined method 'env' in controller specifications for user sign_in

In my spec_helper.rb I added

config.include Devise::TestHelpers, :type => :controller 

In the controller specification before: all hook I'am trying to log in using this code

  @request.env["devise.mapping"] = Devise.mappings[:user] user = FactoryGirl.create(:confirmed_user) sign_in user 

and get the NoMethodError: undefined method `env 'for nil: NilClass.

Any ideas?

+8
ruby ruby-on-rails ruby-on-rails-4 rspec devise
source share
3 answers

I changed before(:all) to before(:each) and now it works. It looks like an API incompatibility between devise (using the github leading branch) and rspec 2.14.1.

+15
source share

Comment out the line @request.env["devise.mapping"] = Devise.mappings[:user] if you are writing specifications for internal Devise controllers. Link from the section of test assistants in the development of documents .

+1
source share

The controller specification will contain the request object. You should probably change from @request for the request.

  request.env["devise.mapping"] = Devise.mappings[:user] 
0
source share

All Articles