title: Testing Sessions in Rails 3 with Rspec & Capybara
body: <p>I'm trying to write integration tests with rspec, factory_girl &amp; capybara. I also have cucumber installed, but I'm not using it (to my knowledge).</p>
  
  <p>I basically want to prepopulate the db with my user, then go to my home page and try to log in. It should redirect to user_path(@user).</p>
  
  <p>However, sessions don't seem to be persisted in my /rspec/requests/ integration tests.</p>
  
  <p>My spec: /rspec/requests/users_spec.rb</p>
  
  <pre><code>require 'spec_helper'
  
  describe "User flow" do
  
    before(:each) do
      @user = Factory(:user)
    end
  
    it "should login user" do
  
      visit("/index")
  
      fill_in :email, :with =&gt; @user.email
      fill_in :password, :with =&gt; @user.password
      click_button "Login"
      assert current_path == user_path(@user)
    end
  end
  </code></pre>
  
  <p>Returns:</p>
  
  <pre><code>Failures:
  
    1) User flow should login user
       Failure/Error: assert current_path == user_path(@user)
       &lt;false&gt; is not true.
       # (eval):2:in `send'
       # (eval):2:in `assert'
       # ./spec/requests/users_spec.rb:16
  </code></pre>
  
  <p>Instead, it redirects to my please_login_path - which should happen if the login fails for any reason (or if session[:user_id] is not set).</p>
  
  <p>If I try to put session.inspect, it fails as a nil object.</p>
  
  <p>If I try to do this in the controller tests (/rspec/controllers/sessions_spec.rb), I can access the session with no problem, and I can call session[:user_id]</p>