title: Devise Rspec expected the response to have a success status code (2xx) but it was 302
body: <p>I have a <code>user</code> which I'm creating with FactoryGirl which needs to have a <code>company</code> in order to login successfully to my <code>root_url</code>.</p>
  
  <p>I'm not having any luck at all stubbing the user method to login.  I've followed <a href="http://willschenk.com/setting-up-testing/" rel="nofollow noreferrer">this tutorial</a> for the Devise portion of the user and needed to amend it a little since my user also requires a <code>company</code> to be associated to it.</p>
  
  <p>I've now created a new model/controller called <code>Scans</code> that is behind Devise's <code>authenticate</code> filter and my first pass at testing it failing with: </p>
  
  <pre><code>5) ScansController GET #show returns http success
       Failure/Error: expect(response).to have_http_status(:success)
         expected the response to have a success status code (2xx) but it was 302
       # ./spec/controllers/scans_controller_spec.rb:32:in `block (3 levels) in &lt;top (required)&gt;'
       # ./spec/spec_helper.rb:127:in `block (3 levels) in &lt;top (required)&gt;'
       # ./spec/spec_helper.rb:126:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>The spec is currently:</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe ScansController, type: :controller do
  
    before(:all) do
      @user = build(:user)
      @company = build(:company)
      @device = build(:device)
      @scan = build(:scan)
    end
  
    describe "GET #show" do
      it "returns http success" do
        login_with @user
        get :show, :device_id =&gt; @device.id, :id =&gt; @scan.id
        expect(response).to render_template(:show)
      end
    end
  </code></pre>
  
  <p>I'm doing a <code>puts</code> on the response, because I want to see what's being returned:</p>
  
  <pre><code>ScansController
    GET #show
  302
  {"X-Frame-Options"=&gt;"SAMEORIGIN", "X-XSS-Protection"=&gt;"1; mode=block", "X-Content-Type-Options"=&gt;"nosniff", "Location"=&gt;"http://test.host/login", "Content-Type"=&gt;"text/html; charset=utf-8"}
  #&lt;Rack::BodyProxy:0x007fb52a7407c0&gt;
  </code></pre>
  
  <p>So, I'm being redirected back to my login page, which tells me that my <code>login_with</code> method in <code>ControllerHelpers</code> is not working correctly:</p>
  
  <pre><code>module ControllerHelpers
    def login_with(user = double('user'), scope = :user)
      current_user = "current_#{scope}".to_sym
      if user.nil?
        allow(request.env['warden']).to receive(:authenticate!).and_throw(:warden, {:scope =&gt; scope})
        allow(controller).to receive(current_user).and_return(nil)
      else
        allow(request.env['warden']).to receive(:authenticate!).and_return(user)
        allow(controller).to receive(current_user).and_return(user)
      end
    end
  end
  </code></pre>
  
  <p>Now, my login functionality does currently work (testing manually).  The first controller that fires after <code>ApplicationController</code> is <code>PagesController#home</code>:</p>
  
  <pre><code>def home
    if current_user &amp;&amp; current_user.company
       verify_subscription
       ....
    else
       redirect_to new_company_path
    end
  end
  </code></pre>
  
  <p><em>If <code>verify_subscription</code> fails the user is also sent to <code>new_company_path</code>, so that doesn't seem to be related to this issue.</em></p>
  
  <p>Based off my rudimentary rspec capabilities, am I right to assume that I'm not even getting close to mimicking a login? If not, what am I doing wrong?</p>