Pat Maddox
2005-Jul-10 06:52 UTC
Database doesn''t revert to default when doing unit tests
As I understand it, every time I run unit tests, all the tables in the DB should be emptied, then populated with whatever fixtures I''ve got set up. Not working for me that way though. In one of my tests, I create a record and save it. Each time I run it, it adds a new record. So after running it 5 times, there are 5 identical records in the db, just with different ids. I thought that it should get wiped out every time I run the tests, so at the end there''d only be one record. I''m using the test db, RAILS_ENV is set to test...I''m so confused.
Jeremy Kemper
2005-Jul-10 07:45 UTC
Re: Database doesn''t revert to default when doing unit tests
On Jul 9, 2005, at 11:52 PM, Pat Maddox wrote:> As I understand it, every time I run unit tests, all the tables in the > DB should be emptied, then populated with whatever fixtures I''ve got > set up. Not working for me that way though. In one of my tests, I > create a record and save it. Each time I run it, it adds a new > record. So after running it 5 times, there are 5 identical records in > the db, just with different ids. I thought that it should get wiped > out every time I run the tests, so at the end there''d only be one > record. I''m using the test db, RAILS_ENV is set to test...I''m so > confused.You must declare your fixtures; simply creating the yaml files is not enough. To exercise model Foo backed by table foos in the unit test for model Bar, augment the test class in test/unit/bar_test.rb: class BarTest < Test::Unit::TestCase # fixtures :bars fixtures :bars, :foos end Now the fixtures defined in test/fixtures/foos.yml will be deleted and reinserted before each test. Best, jeremy