Why is my RSpec not loading Devise::Test::ControllerHelpers?

devise::test::controllerhelpers rspec
rspec devise sign_in not working
rspec devise authenticate_user
devise could not find the `warden::proxy` instance on your request environment.
rspec controller test
rspec functional tests
rails testing rspec
rspec devise confirmation

I'm using Rails 5, and Devise 3.5.1.

Going through a nice (older) book about creating/testing an API, which uses Devise authentication. It was written before Rails 5, so I chose not to use the new api-only version.

Here's my test...

#/spec/controllers/api/v1/users_controller_spec.rb    

require 'rails_helper'

describe Api::V1::UsersController, :type => :controller do
    before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
    describe "GET #show" do
        before(:each) do
            @user = FactoryGirl.create :user
            get :show, params: {id: @user.id}, format: :json
        end
        it "returns the information about a reporter on a hash" do
            user_response = JSON.parse(response.body, symbolize_names: true)
            expect(user_response[:email]).to eql @user.email
        end
        it { should respond_with 200 }
    end
end

And here's a completely unexpected RSpec error

Devise::MissingWarden:
       Devise could not find the `Warden::Proxy` instance on your request environment.
       Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
       If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.

So I go here - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers

and tried this -> include Devise::Test::ControllerHelpers

which didn't help because the file controller_helpers.rb is nowhere in my project

What did I miss here?

Thanks

You could add the following to your rails_helper:

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
end

This will include Devise::Test::ControllerHelpers module in all :controller specs.

ruby on rails, Why is my RSpec not loading Devise::Test::ControllerHelpers? Ask Question It was written before Rails 5, so I chose not to use the new api-only version. Note: Remember to explicitly add the require command in your controller spec file to load the support files. RSpec does not automatically load the files in the support folder anymore. Mappings. Every time you want to unit test a devise controller, you need to tell Devise which mapping to use.

In the spec_helper.rb add:

config.include Devise::Test::ControllerHelpers, :type => :controller

How To: Test controllers with Rails (and RSpec) · heartcombo , class SomeControllerTest < ActionController::TestCase # For Devise >= 4.2.0 include Devise::Test::ControllerHelpers # Use the following instead if RSpec does not automatically load the files in the support folder anymore. After setting up a new app using Rails 4.2.5 with Devise 4.1.1 and Warden 1.2.6, I created a User model authenticating with email and password. I'm trying to setup the spec helpers as per the README.

MiniTest, Rails 4

This works for vanilla Rails 4 MiniTest

test\controllers\users_controller_test.rb
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  include Warden::Test::Helpers
  include Devise::Test::ControllerHelpers

  setup do
    # https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
    # @user = users(:admin)
    # sign_in @user
  end

  teardown do
    Warden.test_reset!
  end

  test "login as admin" do
    @user = users :admin
    sign_in @user
    get :dashboard
    assert_redirected_to admin_dashboard_path
  end

end

ruby-on-rails: Why is my RSpec not loading Devise::Test , It was written before Rails 5, so I chose not to use the new api-only version.Here's my test. Why is my RSpec not loading Devise::Test::ControllerHelpers? I've also hit this with devise 4.2.0, rails 5.0.0 and rspec-rails 3.5.1. This occurs when trying to run my controller specs. Rolling back to devise 4.1.0 and changing

Why is my RSpec not loading Devise::Test::ControllerHelpers?, You could add the following to your rails_helper : RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :controller end This will  Automatically Adding Metadata RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. This was both confusing to new users and not desirable for some veteran users. In RSpec 3, this behavior must be explicitly enabled:

Testing the Blog with RSpec, Add the gem rspec-rails to the development and test group in your Gemfile so it looks like that isn't tested we have to manually generate a spec to test our comment model. One of the amazing things about working with Rails there's a very quick type: :controller config.include Devise::Test::ControllerHelpers, type: :view  I am trying to use the application.html.erb file to create a navbar on all of my pages. I want some of the links to only show if a user is logged in, and some to show if a user is not logged in, so I

Trouble getting api controller testing working, I'm trying to write a controller test for my Coffees Controller. Failure/Error: RSpec.describe Api::V1::CoffeesController, type: Devise::MissingWarden: Devise could not find the `Warden::Proxy` instance on your request environment. that your tests are using the `Devise::Test::ControllerHelpers` module  If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the Devise::Test::ControllerHelpers module to inject the request.env['warden'] object for you.

Comments
  • According to this answer, you cannot use Warden/Devise in a controller test (ActionController::TestCase) because it is Rack middleware and the Rack middleware is not loaded for controller tests. stackoverflow.com/questions/13420923/…
  • OK that makes sense. Thanks sir. The type: :controller is there just to save memory when RSpec is running other types of tests?
  • Devise::Test::ControllerHelpers is intended to be used only in controller tests, so I think some of other specs might be broken if it's included in all of them. That's why we need to specify type: :controller.
  • This is helpful even for people not using rspec. +1
  • for those reading in future - if you're using rails - consider putting this in your rails_helper.