[Rails] Design flaw with Fixtures?

Craig Hockenberry
2006-01-06T22:45:27Z

I''ve been trying to add unit testing for collection of models with
complex relationships -- and failing miserably.

Because of the complexity of the relationships, I''m using a database
which validates foreign key constraints (PostgreSQL 8).

I''m finding it nearly impossible to generate tests that are independent
of each other. The basic problem is the DELETE then INSERT nature of
#create_fixtures.

What happens is that one test inserts a bunch of data into the database
and then exits. The next test comes along and tries to perform the
deletes and an exception is raised because one of the fixtures from the
previous test causes a constraint violation.

Of course, there are ways to avoid these problems, but they are either
non-DRY or have a performance impact:

1) Add #delete_all on all the models during #setup or #teardown. This
cleans up the tables and allows the tests to run.

2) Ensure that all models involved in the relationship are a part of the
#fixtures invocation. When #create_fixtures is run, the contents of all
tables will be deleted.

The more I look at this problem, the more I see a design flaw in how
fixtures are loaded. The current mechanism is:

* nuke any existing fixture data
* load the fixture data
* load the instance variables
* run the test

It seems that a smarter way to perform the tests would be to:

* load the fixture data
* load the instance variables
* run the test
* nuke the fixture data

As it is now, I''m spending more times trying to get fixtures to work
and
no time writing tests. The really frustrating thing is that these
complex relationships is the part of the system that need the most
testing.

After 4 months of using Rails, this is the first time I feel let down...

-ch


--
Posted via http://www.ruby-forum.com/.