title: How to stub a third party object using rspec-mock?
body: <p>I'm kinda new to <code>rspec-mock</code>, now I'm integrating <a href="https://github.com/tatemae-consultancy/hominid" rel="nofollow noreferrer">hominid</a> to communicate with Mailchimp, I want to stub some method calls, so my request doesn't actually go to Mailchimp. Here is what I'm doing</p>
  
  <p><strong>config/initilizers/mailchimp.rb</strong></p>
  
  <pre><code>MAILCHIMP = Hominid::API.new(ENV['MAILCHIMP_API_KEY'])
  LIST_ID   = ENV['MAILCHIMP_LIST_ID']
  </code></pre>
  
  <p><strong>user.rb</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    before_create :add_subscription
    private
      def add_subscription
        MAILCHIMP.list_subscribe(LIST_ID, .......) # Some params following
      end
  end
  </code></pre>
  
  <p>So as you see, for every user created, a <code>list_subscribe</code> will be call on <code>MAILCHIMP</code>, now I don't want <code>list_subscribe</code> was actually called in every test because <code>User</code> was used everywhere, so I config globally a stub like this:</p>
  
  <p><strong>spec/spec_helper.rb</strong> </p>
  
  <pre><code>RSpec.configure do |config|
    config.before(:each) do
      allow_any_instance_of(Hominid::API).to receive(:list_subscribe).and_return(true)
    end
  end
  </code></pre>
  
  <p>I expected <code>MAILCHIMP</code> instance doesn't call <code>list_subscribe</code> but just return <code>true</code>, because if <code>list_subscribe</code> was called, some unexpected requests will go to Mailchimp, I want to prevent this. But my above code doesn't work, what I'm wrong? How to fix this?</p>
  
  <p>Moreover, an answer is likely similar to my case here <a href="https://stackoverflow.com/questions/20218290/how-should-i-stub-a-method-globally-using-rspec">How should I stub a method globally using RSpec?</a></p>
  
  <p>I appreciate any help &amp; suggestion? Thanks!</p>