title: Stubbing API calls in controller with rspec
body: <p>I am just a little confused at why I can't stub a local variable in my controller spec.</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>Class UsersController &lt; ApplicationController
      ...
      def get_company
          resp = Net::HTTP.get("http://get_company_from_user_id.com/#{params[:id]}.json")
          @resp = JSON.parse(resp.body)
          ...
  </code></pre>
  
  <p>My spec looks like:</p>
  
  <pre><code>class ResponseHelper
      def initialize(body)
          @body = body
      end
  end
  
  describe "Get company" do
  it "returns successful response" do
          stub_resp_body = '{"company": "example"}' 
          stub_resp = ResponseHelper.new(stub_resp_body)
      controller.stub!(:resp).and_return(stub_resp)
      get :get_company, {:id =&gt; @test_user.id}
      expect(response.status).to eq(200)
      end
  end
  </code></pre>
  
  <p>I still get an error saying that:</p>
  
  <pre><code> Errno::ECONNREFUSED:
   Connection refused - connect(2)
  </code></pre>
  
  <p>What am I doing wrong? If I am stubbing the <code>resp</code> variable why is it still trying to do the HTTP request and how would I stub the <code>resp</code> variable in this case?</p>