title: Rspec: How to test an exception which is getting raised in private method?
body: <p>In my public method #recalculate, calling the private <code>method1</code>. This method throw exception 'ActiveRecord::StaleObjectError'. </p>
  
  <pre><code>def recalculate
    method_1
    self.save!
  end
  
  private
  def method_1
      begin
        ####
        ####
        if self.lock_version == Product.find(self.id).lock_version
           Product.where(:id =&gt; self.id).update_all(attributes)
        else
           raise ActiveRecord::StaleObjectError.new(self, "test")
        end
      rescue ActiveRecord::StaleObjectError =&gt; e
          if tries &lt; 3
             tries += 1
             sleep(1 + tries)
             self.reload
             retry
          else
             raise Exception.new(timeout.inspect)
          end
      end
  end
  </code></pre>
  
  <p>Rspec Test case:</p>
  
  <pre><code>  it 'if car is updated then ActiveRecord::StaleObjectError should be raised' do
      prod_v1 =Product.find(@prod.id)
      prod_v2 = Car.find(@prod.id)
      prod_v1.recalculate
      prod_v1.reload  # will make lock_version of prod_v1 to 1
  
      prod_v2.recalculate # howvever lock_version of prod_v2 is still 0.
  
      expect{ prod_v2.send(:method1)}.to raise_error(ActiveRecord::StaleObjectError)
  </code></pre>
  
  <p>Error:</p>
  
  <p>Failure/Error: expect(prod_v2.send(:method1)).to raise_error(ActiveRecord::StaleObjectError)
         expected ActiveRecord::StaleObjectError but nothing was raised</p>
  
  <p>Please suggest how to write the unit test case for an exception which is raised in private method.
  I have used <code>send</code> based on the <a href="https://medium.freecodecamp.org/testing-private-methods-in-ruby-using-rspec-c3d429fcd25a" rel="nofollow noreferrer">link</a>: </p>
  
  <p>Note: Exception was raised for in the first time because self.lock_version == Product.find(self.id) was false . And in retry self.lock_version == Product.find(self.id) is true so exception is not capture.</p>