title: devise and rspec-rails - How to sign-in user in Request type specs (specs tagged with type: :request)?
body: <p><strong>Environment</strong></p>
  
  <pre><code>Rails 4.2.0
  ruby-2.2.1 [ x86_64 ]
  devise         3.4.1
  rspec-core  3.2.2
  rspec-rails   3.2.1
  </code></pre>
  
  <p>In my <strong>/spec/rails_helper.rb</strong> I have included <strong>Devise</strong> helpers for spec files tagged with <code>type: :controller</code> and <code>type: :request</code></p>
  
  <p><strong>spec/rails_helper.rb</strong></p>
  
  <pre><code>ActiveRecord::Migration.maintain_test_schema!
  
  RSpec.configure do |config|
    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
  
    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
  
    config.use_transactional_fixtures = false
  
    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end
  
    config.before(:suite) do
      begin
        DatabaseCleaner.start
        FactoryGirl.lint
      ensure
        DatabaseCleaner.clean
      end
    end
  
    config.around(:each) do |example|
      DatabaseCleaner.cleaning do
        example.run # ==================&gt; L-60
      end
    end
  
    config.include FactoryGirl::Syntax::Methods
  
    # RSpec Rails can automatically mix in different behaviours to your tests
    # based on their file location, for example enabling you to call `get` and
    # `post` in specs under `spec/controllers`.
    #
    # You can disable this behaviour by removing the line below, and instead
    # explicitly tag your specs with their type, e.g.:
    #
    #     RSpec.describe UsersController, :type =&gt; :controller do
    #       # ...
    #     end
    #
    # The different available types are documented in the features, such as in
    # https://relishapp.com/rspec/rspec-rails/docs
    config.infer_spec_type_from_file_location!
  
    config.include Devise::TestHelpers, type: :controller
    config.include Devise::TestHelpers, type: :request
  
  end
  </code></pre>
  
  <p>With that config in place the <code>type: controller</code> specs runs fine. However when running <code>type: request</code> specs I am getting following error:</p>
  
  <pre><code> Failure/Error: Unable to find matching line from backtrace
   NoMethodError:
     undefined method `env' for nil:NilClass
   # /home/.rvm/gems/ruby-2.2.1@myapp/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
   # ./spec/rails_helper.rb:60:in `block (3 levels) in &lt;top (required)&gt;'
   # /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/generic/base.rb:15:in `cleaning'
   # /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/base.rb:92:in `cleaning'
   # /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
   # /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `call'
   # /home/.rvm/gems/ruby-2.2.1@simplyhomeapp/gems/database_cleaner-1.4.1/lib/database_cleaner/configuration.rb:87:in `cleaning'
   # ./spec/rails_helper.rb:59:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p><a href="https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb#L24" rel="noreferrer">https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb#L24</a> is following</p>
  
  <pre><code>def setup_controller_for_warden #:nodoc:
    @request.env['action_controller.instance'] = @controller  # ==================&gt; L-24
  end
  </code></pre>
  
  <p>I am aware that @request instance is not available for :request type specs and hence the error.</p>
  
  <p>Are there any helpers available we can use to sign-in a user in :request type specs  when using Devise?</p>
  
  <p>I found a similar issue <a href="https://github.com/plataformatec/devise/issues/1114" rel="noreferrer">https://github.com/plataformatec/devise/issues/1114</a>, the <a href="https://github.com/plataformatec/devise/issues/1114#issuecomment-1310379" rel="noreferrer">reply</a> to which suggests following:</p>
  
  <blockquote>
    <p>If you're doing integration tests, make sure to sign in your user in the tradicional way, by filling the sign in form and submitting.</p>
  </blockquote>
  
  <p>But I would like to by pass the actual login for specs which requires a signed-in user.</p>
  
  <p>Thanks.</p>