title: Rspec/Rails 4, access current_user from Rspec.feature spec?
body: <p>I'm able to access current_user from controller specs no problem. From within an Rspec.feature spec, I'm having trouble making current_user available.</p>
  
  <p>The reason I need a current_user is, I am testing a Send Invitation feature. I navigate to the page from which invitations are sent, fill in the invite[to_user] field, and click 'Send Invitation'. I want to make sure that the proper flash message is being display on the page. However, I am setting the sender_id to current_user id in the controller action that is triggered by clicking on 'Send Invitation' - here is where I need current_user to be defined.</p>
  
  <p>I set up support/controller_macros.rb for controller tests like so:</p>
  
  <pre><code>module ControllerMacros
    def login_user
      before(:each) do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        user = FactoryGirl.create(:user)
        sign_in user
      end
    end
  end
  </code></pre>
  
  <p>support/devise.rb</p>
  
  <pre><code>RSpec.configure do |config|
    config.include Devise::Test::ControllerHelpers, :type =&gt; :controller
      config.extend ControllerMacros, :type =&gt; :controller
  end
  </code></pre>
  
  <p>login_user is not available inside of the Rspec.feature spec...which makes sense, becasue the type is 'feature' not 'controller'. I'm assuming that is the reason why. </p>
  
  <p>I figured I should be able to call sign_in from with the Rspec.feature spec. But that doesn't work...the sign_in method isn't available either. Here is the spec I am trying to get working:</p>
  
  <pre><code>require "rails_helper"
  
  RSpec.feature "Carpool Invitations", :type =&gt; :feature do
      let!(:carpool) { FactoryGirl.create(:carpool)}
      let!(:user) { FactoryGirl.create(:user)}
  
  
    scenario "User sends a new carpool invite" do
          sign_in user
  
      visit new_carpool_invite_path(carpool)
      fill_in "invite[to_user]", :with =&gt; "matthewalexander108@gmail.com"
      click_button "Send Invitation"
      expect(page).to have_text("Invitation was successfully sent!")
    end
  end
  </code></pre>
  
  <p>Is there a way to configure Rspec.feature specs so it can see, and use, the login_user method define in the support/controller_macros.rb file? I've looked at:</p>
  
  <p><a href="https://stackoverflow.com/questions/10039253/why-i-can-not-get-current-user-while-writing-test-case-with-rspec-and-capybara">Why i can not get current_user while writing test case with Rspec and Capybara</a></p>
  
  <p>but I wasn't able to get what I need.</p>
  
  <p>UPDATE:</p>
  
  <p>My question is different because I am running an integration test, ultimately I am testing the content of page. The integration test I am running involves sending an invite, the sending invite action creates an invite object, in the process of creating an invite,  current_user.id is added as the <code>sender_id</code> of the invite object being created. Once the invite is successfully created, the same page reloads with a flash message. </p>
  
  <p>I am testing the flash message.</p>
  
  <p>In order to do this, I need current_user to be defined in the action that creates the invite.</p>