title: How to do integration testing with RSpec and Devise/CanCan?
body: <p>If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the status returns 200 for that url?</p>
  
  <pre><code>def login(user)
    post user_session_path, :email =&gt; user.email, :password =&gt; 'password'
  end
  </code></pre>
  
  <p>This was pseudo-suggested in the answer to this question: <a href="https://stackoverflow.com/questions/5787409/stubbing-authentication-in-request-spec">Stubbing authentication in request spec</a>, but I can't for the life of me get it to work with devise.  CanCan is receiving a nil User when checking Ability, which doesn't have the correct permissions, naturally.</p>
  
  <p>There's no access to the controller in integration specs, so I can't stub current_user, but I'd like to do something like this.</p>
  
  <pre><code>describe "GET /users" do
    it "should be able to get" do
      clear_users_and_add_admin #does what it says...
      login(admin)
      get users_path
      response.status.should be(200)
    end
  end
  </code></pre>
  
  <p><strong>NOTE!!!</strong>:  all this has changed since the question was asked.  The current best way to do this is here: <a href="http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara" rel="nofollow noreferrer">http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara</a></p>