title: How do I simulate a login with RSpec?
body: <p>I have been playing with Rails for a couple of years now and have produced a couple of passable apps that are in production. I've always avoided doing any testing though and I have decided to rectify that. I'm trying to write some tests for an app that I wrote for work that is already up and running but undergoing constant revision. I'm concerned that any changes will break things so I want to get some tests up and running. I've read the RSpec book, watched a few screencasts but am struggling to get started (it strikes me as the sort of thing you only understand once you've actually done it).</p>
  
  <p>I'm trying to write what should be a simple test of my ReportsController. The problem with my app is that pretty much the entire thing sits behind an authentication layer. Nothing works if you're not logged in so I have to simulate a login before I can even send forth a simple get request (although I guess I should write some tests to make sure that nothing works without a login - I'll get to that later).</p>
  
  <p>I've set up a testing environment with RSpec, Capybara, FactoryGirl and Guard (wasn't sure which tools to use so used Railscasts' suggestions). The way I've gone about writing my test so far is to create a user in FactoryGirl like so;</p>
  
  <pre><code>FactoryGirl.define do
    sequence(:email) {|n| "user#{n}@example.com"}
    sequence(:login) {|n| "user#{n}"}
    factory :user do
      email {FactoryGirl.generate :email}
      login {FactoryGirl.generate :login}
      password "abc"
      admin false
      first_name "Bob"
      last_name "Bobson"
    end
  end
  </code></pre>
  
  <p>and then write my test like so;</p>
  
  <pre><code>require 'spec_helper'
  
  describe ReportsController do
    describe "GET 'index'" do
      it "should be successful" do
        user = Factory(:user)
        visit login_path
        fill_in "login", :with =&gt; user.login
        fill_in "password", :with =&gt; user.password
        click_button "Log in"
        get 'index'
        response.should be_success
      end
    end
  end
  </code></pre>
  
  <p>This fails like so;</p>
  
  <pre><code>  1) ReportsController GET 'index' should be successful
       Failure/Error: response.should be_success
         expected success? to return true, got false
       # ./spec/controllers/reports_controller_spec.rb:13:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>Interestingly if I change my test to <code>response.should be_redirect</code>, the test passes which suggests to me that everything is working up until that point but the login is not being recognised.</p>
  
  <p>So my question is what do I have to do to make this login work. Do I need to create a user in the database that matches the FactoryGirl credentials? If so, what is the point of FactoryGirl here (and should I even be using it)? How do I go about creating this fake user in the testing environment? My authentication system is a very simple self-made one (based on <a href="http://railscasts.com/episodes/250-authentication-from-scratch" rel="noreferrer">Railscasts episode 250</a>). This logging in behaviour will presumably have to replicated for almost all of my tests so how do I go about doing it once in my code and having it apply everywhere?</p>
  
  <p>I realise this is a big question so I thank you for having a look. </p>