Guardia Alata
2007-Feb-10 08:32 UTC
Problems with UnitTest on a class with a transaction in it.
Hi, i''ve problems trying to test a simple class with a transaction in it. The class is a simple "account" class like this: class Account < ActiveRecord::Base def increase(value) self.value = self.value + value self.save end def decrease(value) raise ArgumentError,"no founds" if value > self.value self.value = self.value - value self.save end end and the bank class is this: class Bank def transfer(account_1, account_2,value) begin Account.transaction do account_1.increase(value) account_2.decrease(value) end rescue => e raise e end end end Using the class within the rails application, everything runs smoothly. Trying to transfer an amount that exceed the remaining founds, the class raises an exception and the transaction rollbacks correctly. The class is used inside a bank controller, etc... But when i try to UnitTest the same class with a test like this: def test_transfer_failed bank = Bank.new inner = accounts(:one) outer = accounts(:two) inner.update_attribute(:value,200) inner.update_attribute(:value,300) assert_raise(ArgumentError) do bank.transfer(inner,outer,240) end assert_equal 200,inner.value assert_equal 300,outer.value end the test fails, like if the transfer complete and the transaction doesn''t rollbacks. Anyone can explain me why the test fails? Why the class doesn''t rollbacks while inside the test? Any help appreciated, thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy Kemper
2007-Feb-10 09:06 UTC
Re: Problems with UnitTest on a class with a transaction in it.
On 2/10/07, Guardia Alata <guardiaalata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, i''ve problems trying to test a simple class with a transaction in > it.Set self.transactional_fixtures = false at the beginning of your test class. Tests are wrapped in transactions by default so any changes the test makes to the database may be easily rolled back. jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
alessino
2007-Feb-10 12:12 UTC
Re: Problems with UnitTest on a class with a transaction in it.
On Feb 10, 10:06 am, "Jeremy Kemper" <jer...-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> Set self.transactional_fixtures = false at the beginning of your test class. > > Tests are wrapped in transactions by default so any changes the test > makes to the database may be easily rolled back.Thank you for the answer, but i''ve already turned off transactional fixtures in this test. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---