“undefined method `env' for nil:NilClass” in &

2019-01-23 10:29发布

I'm trying to create a spec for a sign out flow by using factorygirl to create a user and then use Devise's sign_in method to authenticate the user, then use capybara to click the "Sign Out" link.

I'm getting (what seems to me to be) a strange error when I run the spec:

Failures:

  1) Sign out flow successfully redirects to the welcome index (root)
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # /home/vagrant/.rvm/gems/ruby-2.0.0-p576/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'

Finished in 0.00226 seconds (files took 3.32 seconds to load)
1 example, 1 failure

Here's the spec:

require 'rails_helper'

describe "Sign out flow" do

  include Devise::TestHelpers

  describe "successfully" do
    it "redirects to the welcome index (root)" do
      user = create(:user)
      sign_in user


      within '.user-info' do
        click_link 'Sign Out'
      end

      expect(current_path).to eq root_path
    end
  end
end

And my user.rb factory:

FactoryGirl.define do
  factory :user do
    name "Fake User"
    sequence(:email, 100) { |n| "person#{n}@example.com" }
    password "helloworld"
    password_confirmation "helloworld"
    confirmed_at Time.now
  end
end

The error seems to be triggered simply from the line include Devise::TestHelpers, as I've tried commenting out the entire content of the spec and still get the same error.

I thought the Devise test helpers would work out of the box; did I miss some configuration? Thanks.

10条回答
【Aperson】
2楼-- · 2019-01-23 11:08

For the sake of being complete, with Rails 5 and RSpec I have run into similar issues when using the latest helpers, as they need to be set explicitly with the type when not used as a superclass.

So if you find yourself receiving there errors in your model tests there's a pretty good chance the type is not set.

Here's what I use in the spec_helper:

  config.include Devise::Test::ControllerHelpers, type: :controllers
  config.include Devise::Test::ControllerHelpers, type: :view
  config.include Devise::Test::IntegrationHelpers, type: :feature

I know that the docs do mention this, but there are times when you may run across an older blog that gives you an older approach, or upgrading from an older setup, and next thing you know this happens.

查看更多
Anthone
3楼-- · 2019-01-23 11:09

Here's my solution:

class ActiveSupport::TestCase
  # all the normal stuff
end

class ActionController::TestCase
  include Devise::TestHelpers    
end
查看更多
看我几分像从前
4楼-- · 2019-01-23 11:12

I meet the same error on rails 5. Here's my solution

spec/rails_helper.rb

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

spec/controllers/your_controller_spec.rb

RSpec.describe YourController, type: :controller do
  before(:all) do
  user = FactoryGirl.create(:user)
  login_as user, scope: :user
end

it "#index" do
  get "index"
  expect(response).to render_template(:index)
  expect(response).to have_http_status(200)
end

$ rspec --tag focus

Run options: include {:focus=>true}
DashboardController
#index
Finished in 3.9 seconds (files took 3.5 seconds to load)
1 example, 0 failures
查看更多
看我几分像从前
5楼-- · 2019-01-23 11:20

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;

查看更多
Melony?
6楼-- · 2019-01-23 11:20

The correct syntax for Rails 5 / Devise (4.2.0) is

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, :type => :controller
end
  • Devise::TestHelpers are deprecated so use Devise::Test::ControllerHelpers
  • :type => :controller - to limit only for controllers and not models for example.
查看更多
疯言疯语
7楼-- · 2019-01-23 11:21

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 :)

查看更多
登录 后发表回答