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

后端 未结 10 2018
陌清茗
陌清茗 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: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
    

提交回复
热议问题