# Bug? ruby tests do not write to mysql-database

**URL:** <https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133>\
**Category:** Rails\
**Created:** [February 23, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133 "2009-02-23T18:12:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/1 "2009-02-23T18:12:20Z")

</div>

Hi,

i have a very strange and serious problem. In my test i create an  
object with (ActiveRecord::Base).create (i also tried .new and .save!)  
everything seems to be fine, assert \*.errors.empty? is ok  
but there is ne entry in the database.

I used the debugger command and put it right after create/save and  
printed out the local variables.  
The strange thing is, that i see a database id in the object, so it  
must be in the db cause its an autoincrement field.

I also double-checked my database.yml settings for test, tried to  
refer to a non existing db and got the right error.

Im really desperate on this please help

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/2 "2009-02-23T21:25:53Z")

</div>

> Are you checking by looking at the database with whatever command line  
> tool your db provides ?  
> tests run inside a transaction and by default other connections can’t  
> see uncommitted changes from another transaction.

I’m using phpMyAdmin to look at the database used for my tests.

That might be a solution, but why does ActiveRecord not commit after  
save! ?  
that does not make any sense to me.

schtief

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/3 "2009-02-23T21:06:29Z")

</div>

On 23 Feb 2009, at 17:09, schtieF wrote:

> Hi,
> 
> i have a very strange and serious problem. In my test i create an  
> object with (ActiveRecord::Base).create (i also tried .new and .save!)  
> everything seems to be fine, assert \*.errors.empty? is ok  
> but there is ne entry in the database.

> I used the debugger command and put it right after create/save and  
> printed out the local variables.

Are you checking by looking at the database with whatever command line  
tool your db provides ?  
tests run inside a transaction and by default other connections can’t  
see uncommitted changes from another transaction.

Fred

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/4 "2009-02-23T21:51:18Z")

</div>

Thats bad, how can i disable this “feature”.  
I also need to look at the database when debugging my tests.

Its not a problem for me to pay the cost of reloading the fixtures  
which i do not use.

thanx in advance

schtief

2009/2/23 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/5 "2009-02-23T21:35:56Z")

</div>

On 23 Feb 2009, at 20:24, schtieF wrote:

> That might be a solution, but why does ActiveRecord not commit after  
> save! ?  
> that does not make any sense to me.

Because in tests the entire test is wrapped in a transaction (so that  
after the test all the changes it made can be rolled back so that  
fixtures don’t have to be reloaded after each test)

Fred

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 24, 2009, 3:42am UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/6 "2009-02-24T03:42:53Z")

</div>

schtieF wrote:

> I also double-checked my database.yml settings for test, tried to  
> refer to a non existing db and got the right error.

Add to your test, after the create line, this…

sleep 80

…and turn off the line in test\_helper.rb that says something about  
transactional fixtures.

Then run the test, go to another shell, and look in the database for  
your record.

The deal is that to preserve test isolation, the fixture system erases  
the  
entire database between each test case. That’s why I offered the sleep  
trick -  
to see the record before the next test zilches it.

Then, because erasing and rebuilding the entire database is inefficient,  
the  
test runner also has the option to build the database once (AFAIK), and  
then run  
each test case in a transaction. When the transaction rolls back, the  
data  
fixtures reappear in the correct state for the text test.

Because one user (actually one database connection) cannot see the  
changes in  
another connection’s transaction until it commits, and because our  
transactions  
will never commit, you must disable the transaction system to see your  
new record.

Going forward, put your sample records into test/fixtures/\*yml, and then  
use  
rake db:fixtures:load to populate your development database for manual  
testing.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![schtieF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@schtieF](https://www.ruby-forum.com/u/schtieF)\
**Post date:** [February 23, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/bug-ruby-tests-do-not-write-to-mysql-database/161133/7 "2009-02-23T22:32:27Z")

</div>

On Feb 23, 8:50 pm, Stefan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thats bad, how can i disable this “feature”.  
> I also need to look at the database when debugging my tests.
> 
> Its not a problem for me to pay the cost of reloading the fixtures  
> which i do not use.

You can turn off transaction fixtures in test\_helper.rb or you can  
change your transaction isolation level.

Fred
