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

The class is a simple "account" class like this:

class Account < ActiveRecord::Base
   def increase(value)
     self.value = self.value + value
   def decrease(value)
     raise ArgumentError,"no founds" if value > self.value
     self.value = self.value - value

and the bank class is this:

class Bank
   def transfer(account_1, account_2,value)
       Account.transaction do
     rescue => e
       raise e

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 =
     inner = accounts(:one)
     outer = accounts(:two)

     assert_raise(ArgumentError) do

     assert_equal 200,inner.value
     assert_equal 300,outer.value

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,


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.