ruby on rails - How to use rspec to test a transaction block -
i wrapping code in transaction block i.e.
tip.transaction ...make changes lots of tips... end
the reason doing want make sure changes made before being committed database. how use rspec test if failure occurs before transaction completed, database rollback previous state?
you can check no tip persisted in case of failure. doubt here failing tip means you, since that's reproduce in test. plain vanilla example:
# app/models/tip.rb before_save :fail_if_bad_amoun def fail_if_bad_amount fail badamount if amount == 'bad' end def self.process(tips) tip.transaction tips.all? &:save end end # spec/models/tip_spec.rb "does not commit in case of failure in 1 of tips" good_tip = tip.new(amount: 1_000) bad_tip = tip.new(amount: 'bad') expect tip.process([good_tip, bad_tip]) end.not_to change { tip.count } end
Comments
Post a Comment