title: RSPEC test index action with before_action filter
body: <p>I have a <code>before_action</code> filter and want to test that the index action is only executed if the user is logged in. Simply put, i don't know how to do this. I'm using my own simple authentication and i know i could use CanCan or similar but for my own learning i'm doing it the hard way!</p>
  
  <p><strong>ApplicationController.rb</strong></p>
  
  <pre><code>helper_method :logged_in
  helper_method :current_user
  
  
  def current_user
  
    @current_user ||= User.find_by_id(session[:current_user]) if session[:current_user]
  
  end
  
  
  
  def logged_in
  
    unless current_user
      redirect_to root_path
    end
  
  end
  </code></pre>
  
  <p><strong>ActivitiesController.rb</strong></p>
  
  <pre><code>before_action :logged_in
  
  def index
    @activities = Activity.all.where(user_id: @current_user)
  end
  </code></pre>
  
  <p><strong>Activities_Controller_spec.rb</strong></p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe ActivitiesController, :type =&gt; :controller do
  
  describe "GET index" do
  
    before(:each) do
      @activity = FactoryGirl.create(:activity)
      session[:current_user] = @activity.user_id
      @current_user = User.find_by_id(session[:current_user]) if session[:current_user]
    end
  
    it "shows all activities for signed in user" do
      get :index, {user_id: @activity.user_id}
      expect(response).to redirect_to user_activities_path
  
    end  
  
  end
  
  
  end
  </code></pre>
  
  <p><strong>activities.rb(Factory)</strong> </p>
  
  <pre><code>FactoryGirl.define do
  
  factory :activity do
  
  association :user
  
    title { Faker::App.name }
    activity_begin { Faker::Date.forward(10) }
    activity_end { Faker::Date.forward(24) }
  
  end  
  
  end
  </code></pre>
  
  <p>I'm getting the following error:</p>
  
  <pre><code>Failure/Error: expect(response).to redirect_to user_activities_path
     Expected response to be a redirect to &lt;http://test.host/users/1/activities&gt; but was a redirect to &lt;http://test.host/&gt;.
     Expected "http://test.host/users/1/activities" to be === "http://test.host/".
  </code></pre>