title: Rspec: How to test retry?
body: <p>I have implemented Optimistic Locking for Race Condition. If <code>lock_version</code> doesn't match with the updated <code>lock_version</code> in the database, then it will trigger <code>retry</code> three times. Can you suggest how to test this <code>retry</code> event </p>
  
  <pre><code>#Product: Model's new field:
      #  lock_version                       :integer(4)      default(0), not null
  
  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 Unit Test:</p>
  
  <pre><code>  it 'if car is updated then ActiveRecord::StaleObjectError should be raised' do
      prod_v1 =Product.find(@prod.id)
      prod_v2 = Product.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{car_v2.send(:method1)}.to receive(:retry)
    end
  </code></pre>