title: How to mock out calls to open-uri
body: <p>I've got a mailer that uses 'open-uri'.</p>
  
  <pre><code>require 'open-uri'
  class NotificationMailer &lt; ActionMailer::Base
  
    def welcome(picasa_picture)
      picture = picasa_picture.content.src
      filename = picture.split('/').last
      attachments.inline[filename] = open(picture).read
      mail(
        to: 'foo@exmample.com',
        from: 'bar@example.com',
        subject: 'hi',
      )
    end
  end
  </code></pre>
  
  <p>But when I try and test anything the class, I get this error:</p>
  
  <pre><code> SocketError:
     getaddrinfo: nodename nor servname provided, or not known
  </code></pre>
  
  <p>I found this SO post: <a href="https://stackoverflow.com/questions/10796986/how-to-rspec-mock-open-uri">How to rspec mock open-uri</a> and thought it would help.  I gave this a try:</p>
  
  <pre><code>let(:pic_content) { double(:pic_content, src: 'http://www.picasa/asdf/asdf.jpeg') }
  let(:picture) { double(:picture, content: pic_content) }
  let(:open_uri_mock) { double(:uri_mock, read: true) }
  
  subject { described_class.welcome(picture) }
  
  it 'renders email address of sender' do
    subject.stub(:open).and_return(open_uri_mock)
    subject.from.should == [ sender_address ]
  end
  </code></pre>
  
  <p>I also tried a 'should_receive' instead of 'stub', but it didn't help.</p>
  
  <p>How do I suppress the open-uri 'open' method so that it (1) doesn't try to go out to internet and (2) doesn't break my tests?</p>