» tagged pages
» logout
User:jerryk
Return to Ruby, Rails and Related

A Surprising Change in Rails 1.0 Involving Test Fixtures and Transactions

Make further edits, (or Cancel)

(or Cancel)

(Editing anonymously: to be credited for your changes, login or register a new account)

(or Cancel)

Tags Applied to this Entry

1 person has tagged this page:

While following the discussion in chapter 12 of Agile Web Development in Rails , using MySQL as my database, I had a few frustrating moments with test fixtures.

I found that the database contents specified in my test fixture were not being restored between the runs of individual test cases, thereby causing every test after the first one that removed something from the database to fail.

Some digging around seemed to show that the behavior had changed in Rails 1.0 and that the text of my printing of the book seems to be, understandably, a bit out of date.

It seems that Rails now, by default, relies on database transactions to restore the database to its pre-test-run state. If your MySQL setup is using the defaut MyISAM engine for its tables, transactions aren’t supported, and the database will not be returned to its initial state in this scheme.

To deal with this problem you have two options:

  • Use MySQL Transactions: Create your database tables using the transaction-supporting InnoDB engine. There’s a good chance you want to do this for other reasons anyway.
  • Change Rails’ Behavior to that of pre-1.0: Edit
    test/test_helper.rb
    in your Rails project to disable transactional fixtures to set
    use_transactional_fixtures=false
    and
    use_instantiated_fixtures=true

This gets things behaving as the book (which BTW is very good) would lead you believe they would.

Username:
Password:
(or Cancel)