“undefined method `env' for nil:NilClass” in 'setup_controller_for_warden' error when testing Devise using Rspec

后端 未结 10 2011
陌清茗
陌清茗 2020-12-09 14:31

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

相关标签:
10条回答
  • 2020-12-09 15:16

    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
    0 讨论(0)
  • 2020-12-09 15:20

    Like others have already said, you're including the Devise::TestHelpers. That's for testing controllers. If you'd still like to automatically login a test user in your integration tests, check out the official Devise Instructions on using it with Capybara.


    Using Devise with Capybara

    Basically, what you need to do is first enable Warden's test mode:

    include Warden::Test::Helpers
    Warden.test_mode!
    

    Then, (create and) login your user:

    user = FactoryGirl.create(:user)
    login_as(user, scope: :user)
    

    Example:

    # spec/features/survey_spec.rb
    require 'rails_helper'
    
    feature 'survey app' do
        include Warden::Test::Helpers
    
        let(:user)   { create(:user) }
        let(:survey) { create(:survey_with_questions) }
    
        before do
            # Sign the User in
            Warden.test_mode!
            login_as(user, scope: user)
        end
    
        it 'renders the survey' do
            visit survey_show_path(survey)
            expect(page).to have_content(survey.title)
        end
    end
    
    0 讨论(0)
  • 2020-12-09 15:21

    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
    
    0 讨论(0)
  • 2020-12-09 15:25

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

    0 讨论(0)
提交回复
热议问题