title: Stubbing result of Net::HTTP.new(url.host, url.port)
body: <p>I have a funciton that calls Net::HTTP.new(google_url.host, google_url.port) and I am trying to figure out how to stub the result for testing. Basically I don't want to be hitting the google URL shortener every time I run my test.</p>
  
  <pre><code> def shorten_url(long_url)
      google_url = URI.parse("https://www.googleapis.com/urlshortener/v1/url")
      data = JSON.generate({"longUrl" =&gt; "#{long_url}"})
      header = {"Content-Type" =&gt; "application/json"}
      http = Net::HTTP.new(google_url.host, google_url.port)
      http.use_ssl = true
  
      short_url = ""
  
      res = http.request_post(google_url.path, data, header)
      jsonResponse = JSON.parse(res.body)
      short_url = jsonResponse["id"]
    end
  </code></pre>
  
  <p>Basically I want to be able to set the result of that function. </p>
  
  <p>I've tried things like: <code>Net::HTTP.any_instance.stubs(:HTTP.new).returns("www.test.com")</code> but cannot figure out how to get it to work. </p>
  
  <pre><code>class HTTP &lt; Protocol
       ....
      # Creates a new Net::HTTP object.
      # If +proxy_addr+ is given, creates an Net::HTTP object with proxy support.
      # This method does not open the TCP connection.
      def HTTP.new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil)
        h = Proxy(p_addr, p_port, p_user, p_pass).newobj(address, port)
        h.instance_eval {
          @newimpl = ::Net::HTTP.version_1_2?
        }
        h
      end
  ....
  end
  </code></pre>