I have implemented Optimistic Locking for Race Condition. For that, I added an extra column lock_version
in the Product. Method: recalculate
is calling
You could write something like:
expect(ActiveRecord::StaleObjectError).to receive(:new).and_call_original
Because you are rescue-ing the exception
Make sure to check https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
expect(car_v2).to receive(:method1).and_raise(ActiveRecord::StaleObjectError)
Means that when car_v2
receives the method1
you won't call it but you'll raise an exception of type ActiveRecord::StaleObjectError
. This is why you also receive the ArgumentError
With rspec you can check that a specific code raises an error (that is not handled in your case it is handled -> rescue...) like this:
expect { my_cool_method }.to raise_error(ErrorClass)