title: How to test if method is called in RSpec but do not override the return value
body: <p>There are already <a href="https://stackoverflow.com/questions/21262309/rspec-test-to-see-if-a-method-was-called">questions</a> similar to this, but they all override the return values to <code>nil</code> unless <code>.and_return</code> is called as  well</p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>I am wondering if there is a way to just check if a method is called using <code>expect_any_instance_of(Object).to receive(:somemethod)</code> and it runs normally without overriding or affecting the return value of <code>.somemethod</code>.</p>
  
  <ul>
  <li>rspec-3.4.0</li>
  <li>rails 4.2</li>
  </ul>
  
  <p>Consider the following:</p>
  
  <pre><code># rspec
  it 'gets associated user' do
    expect_any_instance_of(Post).to receive(:get_associated_user)
    Manager.run_processes
  end
  
  # manager.rb
  class Manager
    def self.run_processes
      associated_user = Category.first.posts.first.get_associated_user
      associated_user.destroy!
    end
  end
  </code></pre>
  
  <p>The spec above although will work because <code>:get_associated_user</code> is called in the <code>run_processes</code>, however it raises <code>NoMethodError: undefined method 'destroy!' for NilClass</code> precisely because I mocked the <code>:get_associated_user</code> for any instance of Post.</p>
  
  <p>I could add a <code>.and_return</code> method like <code>expect_any_instance_of(Post).to receive(:get_associated_user).and_return(User.first)</code> so that it will work without raising that error, but that already is a mocked  return value (which might affect the rest of the  code below it), and not the correct expected value it should have returned at the time the method is called.</p>
  
  <p>I can however specify <code>.and_return(correct_user)</code>  where <code>correct_user</code> is the user that is going to be the same return value as if it has ran normally. However, this will need me to mock every return value in the sequence <code>Category.first.posts.first.get_associated_user</code> just so that it will work normally. The actual problem is a lot more complex than above, therefore stubbing is not really a possible solution in my case.</p>