Forum: Ruby on Rails bug? ruby tests do not write to mysql-database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
schtieF (Guest)
on 2009-02-23 19:12
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-02-23 22:06
(Received via mailing list)
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
schtieF (Guest)
on 2009-02-23 22:25
(Received via mailing list)
> 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
Frederick C. (Guest)
on 2009-02-23 22:35
(Received via mailing list)
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
Stefan L. (Guest)
on 2009-02-23 22:51
(Received via mailing list)
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>:
Frederick C. (Guest)
on 2009-02-23 23:32
(Received via mailing list)
On Feb 23, 8:50 pm, Stefan L. <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
Phlip (Guest)
on 2009-02-24 04:42
(Received via mailing list)
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
This topic is locked and can not be replied to.