title: How do I simulate a session variable in minitest?
body: <p>I'm using Rails 5 and minitest.  I want to test out a controller method that requires a login, validated by the filter in teh controller</p>
  
  <pre><code>  before_filter :require_current_user
  
      def current_user
      @current_user ||= User.find_by(id: session[:user_id])
    end
  
    def require_current_user
      redirect_to(:root, :notice =&gt; "you must be logged in") unless current_user
    end
  </code></pre>
  
  <p>To simulates the session variable, I added this in my test  </p>
  
  <pre><code>  def setup
      @logged_in_user = users(:one)
      session[:user_id] = @logged_in_user.id
    end
  
  test "do create" do
    person = people(:one)
    rating
    post rate_url, params: {person_id: person.id, rating: rating}
  
    # Verify we got the proper response
    assert_response :success
  end
  </code></pre>
  
  <p>But when I run the above test, it results in the error</p>
  
  <pre><code>NoMethodError: undefined method `session' for nil:NilClass
      test/controllers/rates_controller_test.rb:10:in `setup'
  </code></pre>
  
  <p>How do I simulate a session variable in minitest?</p>
  
  <p><b>Edit:</b> Per the response given, here is my session_create method</p>
  
  <pre><code>  def create
      puts "env: #{env["omniauth.auth"]}"
      user = User.from_omniauth(env["omniauth.auth"])
      first_login = user.last_login.nil?
      # Record the fact that this is their first login in the session
      session[:first_login] = first_login
      # Record the last login of the user
      user.last_login = Time.now
      user.save
      session[:user_id] = user.id
  
      last_page_visited = session[:last_page_visited]
      session.delete(:last_page_visited)
      url = last_page_visited.present? ? last_page_visited : url_for(:controller =&gt; 'votes', :action =&gt; 'index')
      redirect_to url
    end
  </code></pre>