"undefined method `env' for nil:NilClass" in 'setup_controller_for_warden' error when testing Devise using Rspec
In Rails 5 you must include Devise::Test::IntegrationHelpers
instead Devise::Test::ControllerHelpers
:
# rails_helper.rb
config.include Devise::Test::IntegrationHelpers, type: :feature
See more:
- https://github.com/plataformatec/devise/issues/3913#issuecomment
- https://github.com/plataformatec/devise/pull/4071
Apparently there are issues with Devise::TestHelpers
and integration testing, so perhaps that's the problem here.
https://github.com/plataformatec/devise (mentioned in README, Issues, etc.; also see related SO questions):
These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session;
FWIW it seems like the issues have been fixed, however I ran into the issue after not reading the documentation well enough.
This was our code:
RSpec.configure do |config|
...
config.include Devise::TestHelpers
...
end
This means every test will include the test helpers, including models. This wound up being the issue for us. Should we have read the documentation closer we would have noticed Devise suggests limiting it to only controllers with:
RSpec.configure do |config|
...
config.include Devise::TestHelpers, type: :controller
...
end
This solved the issue for us. All tests passing :)