Scenario
Have a race case where concurrency can cause a duplicate key error. Take for example:
def before_create_customer_by_external_id
end
def create_customer_from_external_id(external_id = nil)
@customer = current_account.customers.create!(external_id: external_id || @external_id)
end
def set_new_or_old_customer_by_external_id
if @customer.blank?
before_create_customer_by_external_id
create_customer_from_external_id
end
rescue ActiveRecord::RecordInvalid => e
raise e unless Customer.external_id_exception?(e)
@customer = current_account.customers.find_by_external_id(@external_id)
end
The Test
Now, to test the race case (based on the answer to Simulating race conditions in RSpec unit tests) we just need to monkey patch before_create_customer_by_external_id
to call create_customer_from_external_id
.
The Question
How can you do this without overriding the whole class and getting a "method not found" error?