title: How to test that private method is called using rspec
body: <p>I'm trying to make sure that a given method is being called when called by a callback.
  Basically, I have a RiskMatrix model, that calls 2 private methods on callback <code>after_save</code> 
  So I'm trying to test that those methods are properly called.</p>
  
  <p>thanks a lot for your help</p>
  
  <pre><code>class RiskMatrix &lt; ActiveRecord::Base
     after_save :alert_admin, :save_suspicious_ip, if: proc {score.length &gt;= ALERT_THRESHOLD}
  
      private
      def alert_admin
          [...]
      end
  
      def save_suspicious_ip
          [...]
      end
  end
  </code></pre>
  
  <p>risk_matrix_spec.rb</p>
  
  <pre><code> describe 'after_save' do
    context 'score.length &gt; ALERT_THRESHOLD' do
      it 'should run alert_admin' do
        matrix = build(:risk_matrix, score: 'ABCD')
        expect(matrix).to receive(:alert_admin)
      end
  
      it 'should run save_suspicious_ip' do
        matrix = create(:risk_matrix, score: 'ABCD')
        expect(matrix).to receive(:save_suspicious_ip)
      end
  
    end
  end
  </code></pre>
  
  <p>both tests fail</p>
  
  <pre><code> (#&lt;RiskMatrix id: 3201, score: "ABCD", user_id: 3115, created_at: "2019-02-05 16:27:01", updated_at: "2019-02-05 16:27:01"&gt;).alert_admin(*(any args))
      expected: 1 time with any arguments
      received: 0 times with any arguments
  
      (#&lt;RiskMatrix id: nil, score: "ABCD", user_id: nil, created_at: nil, updated_at: nil&gt;).save_suspicious_ip(*(any args))
      expected: 1 time with any arguments
      received: 0 times with any arguments
  </code></pre>