# Problems with UnitTest on a class with a transaction in it

**URL:** <https://www.ruby-forum.com/t/problems-with-unittest-on-a-class-with-a-transaction-in-it/85131>\
**Category:** Rails\
**Created:** [February 10, 2007, 9:32am UTC](https://www.ruby-forum.com/t/problems-with-unittest-on-a-class-with-a-transaction-in-it/85131 "2007-02-10T09:32:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guardia\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f05b48/32.png) [@Guardia\_A](https://www.ruby-forum.com/u/Guardia_A)\
**Post date:** [February 10, 2007, 9:32am UTC](https://www.ruby-forum.com/t/problems-with-unittest-on-a-class-with-a-transaction-in-it/85131/1 "2007-02-10T09:32:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Guardia\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f05b48/32.png) [@Guardia\_A](https://www.ruby-forum.com/u/Guardia_A)\
**Post date:** [February 10, 2007, 10:07am UTC](https://www.ruby-forum.com/t/problems-with-unittest-on-a-class-with-a-transaction-in-it/85131/2 "2007-02-10T10:07:50Z")

</div>

On 2/10/07, Guardia A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Guardia\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f05b48/32.png) [@Guardia\_A](https://www.ruby-forum.com/u/Guardia_A)\
**Post date:** [February 10, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/problems-with-unittest-on-a-class-with-a-transaction-in-it/85131/3 "2007-02-10T13:13:03Z")

</div>

On Feb 10, 10:06 am, “Jeremy K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
