Bug? ruby tests do not write to mysql-database

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

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

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

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. [email protected]:

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

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

On Feb 23, 8:50 pm, Stefan L. [email protected] 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