TL;DR. Writing procedural code within a DB transaction. How can I improve design of the code so it's better testable?
In my application I have a service object that perform multiple things within the same transaction. See code in Ruby below:
class CreateRefundService
def create(customer_id)
ActiveRecord::Base.transaction do
refund = create_refund(customer_id)
credit = add_credits_to_customer(customer_id)
send_email(refund)
add_note(refund, credit)
end
end
private
# ... implementation of all 4 methods above
end
I'm trying to write 4 tests that would check that all four things happen during transaction, but it starts to feel uncomfortable, since for every method under test I need to stub other 3 methods. This gives me a warrant that there's probably a problem about this design.
I'm under constraints that multiple things need to happen during the same transaction.